WPF Style does not affect certain properties - wpf

I've got a Style specified for Paragraph as part of my FlowDocumentReader's Resources section:
<FlowDocumentReader>
<FlowDocumentReader.Resources>
<Style x:Key="myStyle" TargetType="{x:Type Paragraph}">
<Setter Property="Foreground" Value="LightSteelBlue" />
<Setter Property="BorderBrush" Value="LightSteelBlue" />
<Setter Property="BorderThickness" Value="1.0" />
<Setter Property="FontStyle" Value="Italic" />
<Setter Property="FontSize" Value="{Binding Path=MyFontSize}" />
</Style>
</FlowDocumentReader.Resources>
</FlowDocumentReader>
I've got a .xaml file which contains my FlowDocument and it has some Paragraphs which are defined as so:
<Paragraph Style='{DynamicResource myStyle}">
Stuff here
</Paragraph>
The problem that I have is that Foreground doesn't apply to the text (it shows as Black rather than LightSteelBlue) and the FontSize does not change when the MyFontSize property is modified.
I've checked the property value in the code behind and it is set but it doesn't result in a change in the UI.
This seems to only be an issue with the FlowDocument if it is loaded into the FlowDocumentReader at runtime. If the XAML is explicitly placed inside the FlowDocumentReader in the .xaml file, the Foreground is the correct color and the FontSize changes based on the property setting.
Ideas?
Solved:
As I wrote in my answer below, by moving the Style block into the Resources section of the FlowDocument itself resolves the issue.

Have you tried setting foreground for your paragraph directly? it must be a different propety/attached property which manages contents foreground.

Well, I solved this problem by moving the Style blocks out of the FlowDocumentReader Resources and into the Resources section of the FlowDocument itself. The resulting FlowDocument looks something like so:
<FlowDocument>
<FlowDocument.Resources>
<Style x:Key="myStyle" TargetType="{x:Type Paragraph}">
<Setter Property="Foreground" Value="LightSteelBlue" />
<Setter Property="BorderBrush" Value="LightSteelBlue" />
<Setter Property="BorderThickness" Value="1.0" />
<Setter Property="FontStyle" Value="Italic" />
<Setter Property="FontSize" Value="{Binding Path=MyFontSize}" />
</Style>
</FlowDocument.Resources>
<Paragraph Style="{DynamicResource myStyle}">
Stuff here
</Paragraph>
</FlowDocument>

Related

WPF - Image source in a Style in another assembly

I have a library with some WPF Styles and another application that references this library and it styles, but I'm having a problem with an Image style. I set the Source property in the style with an image in the same library, but when I run my application it returns a XAML Markup error saying it didn't found the image.
Library/Styles/MyStyles.xaml (the .png is in Library/Resources/)
<Style TargetType="Image" x:Key="MyToolTipImage">
<Style TargetType="Image" x:Key="SmartToolTipImage">
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="Height" Value="12" />
<Setter Property="Width" Value="12" />
<Setter Property="Margin" Value="0, 0, 10, 0" />
<Setter Property="Source" Value="pack://application:,,,/Library;component/Resources/question_icon.png" />
</Style>
MyProject/View/ConfigView.xaml
<Image
Grid.Row="0" Grid.Column="1"
Style="{library:MyStyleRef ResourceKey=MyToolTipImage}"
ToolTip="{DynamicResource foldersReloadToolTip}"/>
The error returned is Could not find the resource question_icon.png. Also, all others styles that I have defined in this .xaml works well, like buttons and datagrids.
I resolved this by referencing this way
/Library;Component/Resources/question_icon.png
With the first / and the Component with a capital C. Also, the image must be have the Build Action set as Resource.

WPF FlowDocument :: How Can I Define Custom Templates for the Child Paragraph, Run, etc

I'm fairly new to WPF, so please bear with me.
I am trying to customize the presentation of the various elements of a FlowDocument in a RichTextBox (RTB). WPF controls are lookless, so as I understand it I should be able to define the look for every child in the Document.
Would I define the template for this in the RTB? Separate templates for each item (Paragraph, Section, Run, etc) as resources? Let's say, for sake of argument, that I want a red border around every Section, the word "Paragraph:" before every Paragraph with a Margin="5", and every Run to be in Consolas. (see edit one)
EDIT ONE:
I learn more here from asking the wrong question than the right one. It seems I never ask what I'm really trying to do the first time out the gate. Who knows, maybe I'll learn.
I am trying to define a DataTemplate or an ItemTemplate (I think) similar to a ListBox. The ultimate goal is a "source view" HTML editor in an RTB. I want a Paragraph XAML element to render like this in the RTB:
<p>
Lorem ipsum stackus overflowum
</p>
... and a Run would render like this:
<span style="font-weight:bold">clarity is important when asking questions</span>
... and so on with the different XAML elements.
Some of that you can do with styles
E.G.
<Paragraph Style="{StaticResource Par2}">
This is not a substitute for training.
</Paragraph>
<Style x:Key="Par1short" TargetType="Paragraph">
<Setter Property="Margin" Value="15,4,0,0" />
</Style>
<Style x:Key="Par2" TargetType="Paragraph">
<Setter Property="Margin" Value="30,7,0,0" />
</Style>
<Style x:Key="Par2short" TargetType="Paragraph">
<Setter Property="Margin" Value="30,4,0,0" />
</Style>
<Style x:Key="Par3" TargetType="Paragraph">
<Setter Property="Margin" Value="45,7,0,0" />
</Style>
<Style x:Key="Par3short" TargetType="Paragraph">
<Setter Property="Margin" Value="45,4,0,0" />
</Style>
<Style x:Key="Par4" TargetType="Paragraph">
<Setter Property="Margin" Value="60,7,0,0" />
</Style>
<Style x:Key="Par4short" TargetType="Paragraph">
<Setter Property="Margin" Value="60,4,0,0" />
</Style>
<Style x:Key="Par5" TargetType="Paragraph">
<Setter Property="Margin" Value="75,7,0,0" />
</Style>
<Style x:Key="Par5short" TargetType="Paragraph">
<Setter Property="Margin" Value="75,4,0,0" />
</Style>
Now the word Paragraph: before every paragraph I don't think you can do that with styles
The short answer is that you can't apply templates to these elements because they don't inherit from ControlTemplate.

Can I get this binding into a style?

I would like to take the xaml I currently have for a ComboBox (below), and condense it into something like the Style also shown below. I think this should work, but I have a 'Type'ing issue and not sure how to resolve it
"Cannot resolve the Style Property 'Margin'. Verify that the owning type is the Style's TargetType, or use Class.Property syntax to specify the Property.)
As I look at the existing ComboBoxStyle (also below) that I'm looking to base this new style off of, I see that I hadn't used x:Type, but it does seem to work.
Is there any reason this new style shouldn't work? What must I change?
Cheers,
Berryl
combo box, as is, working):
<ComboBox
x:Name="cboDepartmentFilter" Style="{StaticResource ComboBoxStyle}"
Margin="{StaticResource FliterPanelItem_Margin}" Width="{StaticResource FilterPanelItem_Width}"
ItemsSource="{Binding Path=DepartmentFilterControl.Choices}"
ToolTip="{Binding DepartmentFilterControlData.ToolTipTitle}"
/>
what I want:
<ComboBox Style="{StaticResource FilterPanelComboBoxStyle}" DataContext="{Binding DepartmentFilterControl}" />
<!- in some resource file ->
<Style x:Key="FilterPanelComboBoxStyle" BasedOn="{StaticResource ComboBoxStyle}">
<Setter Property="Margin" Value="{StaticResource FliterPanelItem_Margin}" />
<Setter Property="Width" Value="{StaticResource FilterPanelItem_Width}" />
<Setter Property="ItemsSource" Value="{Binding Choices}" />
<Setter Property="ToolTip" Value="{Binding ToolTipTitle}" />
</Style>
<!--
This style defines a common margin for items in a filter panel.
-->
150
existing ComboBoxStyle:
<!-- ComboBox Style -->
<Style x:Key="ComboBoxStyle" TargetType="ComboBox">
<Setter Property="Background" Value="{StaticResource headerBrush}" />
...
<Setter Property="Template">
<Setter.Value>
...
</Setter.Value>
</Setter>
<Setter Property="ItemContainerStyle" Value="{StaticResource ComboBoxItemStyle}" />
<Setter Property="IsSynchronizedWithCurrentItem" Value="True" />
</Style>
You still need to specify the TargetType in the derived style. (Or you prefix the properties with "ComboBox.")

How to make WPF DataGrid Column Header transparent

I am trying to make the column header of my WPF Datagrid to be transparent.
I am able to set it to a color without problem, but I can't have it transparent. Here is what I tried:
<Style x:Key="DatagridColumnHeaderStyle" TargetType="{x:Type tk:DataGridColumnHeader}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Foreground" Value="#C2C4C6" />
</Style>
<Style x:Key="DashboardGridStyle" TargetType="{x:Type tk:DataGrid}">
<Setter Property="ColumnHeaderStyle" Value="{StaticResource DatagridColumnHeaderStyle}" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="RowBackground" Value="Transparent" />
</Style>
<tk:DataGrid Style="{StaticResource DashboardGridStyle}" >
...
</tk:DataGrid>
With this code, it seems to take the default brush.
What am I missing?
I used Snoop to take a look at what was happening. It seems that another DataGridColumnHeader is always created behind the one you can modify, and it's not affected by changes on styles. When you set a transparent background, in fact is being correctly applied, so what you see is that ghost header behind (which has the usual grey background).
If you apply a coloured background and play with Opacity, you will see how the two colours are mixed. I don't know if this can be solved.
With the answer from Natxo (thanks!), I was able to find a solution. And it is a simple one too!
Knowing that there was another DataGridColumnHeader behind the one we can modify through the ColumnHeaderStyle, I just had to set a style that will affect all DataGridColumnHeader:
<Style TargetType="{x:Type tk:DataGridColumnHeader}">
<Setter Property="Background" Value="Transparent" />
</Style>

Changing Hyperlink foreground without losing hover color

I'm writing a WPF app, and I want some of my hyperlinks to be the default blue, and others to be green. No problem -- I can just set Foreground:
<TextBlock><Hyperlink Foreground="#0C0">Mark as done</Hyperlink></TextBlock>
The trouble is, when I do this, the hover effect goes away. In a normal Hyperlink, when I move the mouse over the link, it turns red, and when I move the mouse away, it goes back to blue. But in a Hyperlink with the Foreground property assigned, it's always that color -- the red-on-hover is totally suppressed.
How can I change a Hyperlink's color, without losing the default hover behavior and color?
Setting the Foreground directly (as you've done) doesn't work, and setting it in a Style doesn't work either, unless you "derive" that style from the default Hyperlink style (which must include the OnMouseOver trigger). So this works:
<TextBlock>
<Hyperlink>
<Hyperlink.Style>
<Style TargetType="Hyperlink"
BasedOn="{StaticResource {x:Type Hyperlink}}">
<Setter Property="Foreground" Value="#0C0"/>
</Style>
</Hyperlink.Style>
Mark as done
</Hyperlink>
</TextBlock>
Extracting that style back into the Window resources and referencing it with a key would probably make for more-readable XAML, but the above code does the job.
You should build your Hyperlink like this
<TextBlock Width="Auto" HorizontalAlignment="Center">
<Hyperlink Click="ForgotPassword_Clicked">
<TextBlock Text="Forgot Password?"/>
</Hyperlink>
</TextBlock>
And then this style should work for you
<Style TargetType="{x:Type Hyperlink}">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Foreground" Value="Blue" />
<Setter Property="TextBlock.TextDecorations" Value="{x:Null}" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Red" />
<Setter Property="TextBlock.TextDecorations" Value="Underline" />
</Trigger>
</Style.Triggers>
</Style>

Resources