Silverlight cannot find resource in the same dictionary - silverlight

I have the following style defined in the ResourceDictionary of a Silverlight 4.0 application
<Style x:Key="GridSplitterStyle" TargetType="sdk:GridSplitter">
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Stretch" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="Template" Value="{StaticResource GridSplitterTemplate}" />
</Style>
<ControlTemplate x:Key="GridSplitterTemplate" TargetType="sdk:GridSplitter">
<StackPanel Background="Transparent" Height="32">
<!-- ... -->
</StackPanel>
</ControlTemplate>
When I apply the style on my GridSplitter, the style is found and properly applied. However, when linking the Template property to the ControlTemplate defined in the same dictionary file, the following error comes up:
Cannot find a Resource with the Name/Key GridSplitterTemplate
How come Silverlight can find the style but not the template? They are located in the same file...

Static resource references are resolved during Xaml parsing. As a result you cannot have use forward referencing.
Place the control template above the style in document order so that the parser finds "GridSplitterTemplate" first. Then when "GridSplitterStyle" references it, the parser will be able to find it.

Related

How to make an exception for Application.Resources style in a specific Grid.Resources

I am doing small WPF app for my own using Visual Studio, C#, .NET Standard and WPF in this specific project.
I have defined style for all TextBlocks and TextBoxes in Applications.Resources like below.
<Application.Resources>
<Style TargetType="TextBox">
<Setter Property="FontSize" Value="10"/>
</Style>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="10"/>
</Style>
</Application.Resources>
Then in main window I have a grid which contains some buttons.
<Grid>
<Grid.Resources>
<Style TargetType="Button">
<Setter Property="FontSize" Value="50" />
</Style>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="50"/>
</Style>
</Grid.Resources>
<Button Grid.Column="0" Content="DASHBOARD" Command="local:CustomCommands.ShowDashboard"/>
</Grid>
I would like to set for the textblocks/textboxes in this specific buttons a wider font.
I tried for many different syntax but could not manage it. I tried also do define x:Key for this style in Grid.Resources and use it in this specific Button control. This wasn't work either.
Can anyone let me know which way should I let know my application that text in this buttons would have bigger font size?
The TextBlock created for string contents by the ContentPresenter inside the Button template doesn't apply the locally-defined resources, i.e. those in your Grid.
The easiest way to solve your problem would be to explicitly define a TextBlock as the Button's content.
<Grid>
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="50"/>
</Style>
</Grid.Resources>
<Button Grid.Column="0" Command="local:CustomCommands.ShowDashboard">
<TextBlock Text="DASHBOARD" />
</Button>
</Grid>

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.

How to create themes with style inheritance in XAML

I have created a WPF application that uses styles extensively, and in particulare style inheritance (using BasedOn) to describe its appearance. So, throughout the application's main files (in <Application.Resources> and <Control.Resources>) I have defined styles such as the following:
<Color x:Key="MusicTextColor">Red</Color>
<SolidColorBrush x:Key="MusicTextColorBrush" Color="{DynamicResource MusicTextColor}" />
<Style x:Key="MusicText" TargetType="TextBlock">
<Setter Property="FontSize" Value="14" />
<Setter Property="FontFamily" Value="Segoe UI" />
<Setter Property="Foreground" Value="{DynamicResource MusicTextColorBrush}" />
</Style>
<Style x:Key="MusicTitle" BasedOn="{StaticResource MusicText}" TargetType="TextBlock">
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="FontSize" Value="40" />
</Style>
And in the UI I would use it normally like this to theme contents:
<TextBlock Style="{DynamicResource MusicTitle}">Artist:</TextBlock>
<Rectangle Background="{DynamicResource MusicTextColorBrush}" />
Now to theme the application, I am loading a theme.xaml file containing the theme changes in a <ResourceDictionary> at runtime and merge its resource dictionary with the application resources. The file might e.g. contain the following, which is supposed to change the color and font for all "music" labels:
<ResourceDictionary ... >
<Color x:Key="MusicTextColor">Green</Color>
<Style x:Key="MusicText" TargetType="TextBlock">
<Setter Property="FontSize" Value="14" />
<Setter Property="FontFamily" Value="Consolas" />
<Setter Property="Foreground" Value="{DynamicResource MusicTextColorBrush}" />
</Style>
</ResourceDictionary>
The problem is, this does not work. Neither the color change nor the font change is picked up. Note that the font IS actually changed (and this confirms that a resource merge takes place) if I use any of the redefined styles directly (e.g. "MusicText"), but not the derived one. The explanation is most likely that the BasedOn attribute uses a StaticResource binding, so it will not find the redefined styles, even after the merge. As I learned, BasedOn does not allow a DynamicResource binding that I would need here.
To me this looks like a common problem - theming an application with style inheritance at runtime, but I have not found a realistic solution until now.
Of course I could stop using style inheritance at all, but that would result in a lot of redunant code.
Also, I could copy ALL derived styles into the theme, but that would be error-prone and prevent further application updates (without manually updating each theme with each change).
Did I miss something or is there really no convenient solution for this?

Resource Dictionary WPF

I have a resource dictionary in my WPF application which contains the style information for the various controls.
Can it be used like the way we use in CSS in HTML? For example
p
{
margin:20px;
font:Tahoma;
}
this applies to all "p" tags in HTML. We dont have to specifically mention that in the HTML for "p" tag.
Is the same approach applicable in WPF, or do we have to specifically
mention the style
<TextBlock Text="Test" Style="{DynamicResource SomeTextblockStyle}" />
in the XAML
You can certainly set a default style for each type. You can do this within your Generic.xaml, note that I am not providing a key.
<Style TargetType="{x:Type Button}">
<Setter Property="Height" Value="25"/>
<Setter Property="Foreground" Value="White"/>
</Style>
This will style every instance of a Button within your application as such.
If you were go to a XAML file and define an instance of a Button, overriding the Foreground value, that local instance will take precedence over the global style.
<Button Foreground="Black"/>
You can set style like using key
<Style TargetType="{x:Type TextBlock}" x:Key="myStyle">
<Setter Property="Margin" Value="20"/>
<Setter Property="FontFamily" Value="Tahoma"/>
</Style>
And in the Window.Xaml
<TextBlock Text="Hello" Style="{DynamicResource myStyle}"/>

Resources