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.
Related
I'm using custom url font file in wpf (MS Mincho font) to display japanese characters
Here are my code:
<Style x:Key="TextBlockDisplayResource" TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="TextWrapping" Value="Wrap" />
<Setter Property="FontFamily" Value="./fonts/#MS Mincho"/>
<Setter Property="Foreground" Value="Black"/>
</Style>
<TextBlock Canvas.Left="10" Canvas.Top="30" Foreground="Black"
Style="{DynamicResource TextBlockDisplayResource}"
Text="This is black color"></TextBlock>
I have folder in solution "fonts/MS mincho.ttf"
In Visual studio.. I saw the color result of textblock is black. But when i build, the color is lightblue. even i set foreground=black.
I don't know where i was wrong.
When i try to change set url for fontfamily
<Setter Property="FontFamily" Value="pack://application:,/fonts/#MS Mincho"/>
The error is the same
Please let's me know.
Thanks.
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?
I am using the DataGrid2D posted here. I can't seem to figure out a way to align the Text inside the Cells (i.e. right, center, left). For the default WPF4 DataGrid I just set
<Style x:Key="GridTextColumnStyleLeftAligned" TargetType="TextBlock">
<Setter Property="TextAlignment" Value="Left" />
<Setter Property="Margin" Value="2" />
<Setter Property="TextWrapping" Value="WrapWithOverflow" />
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
Here TextAlignment does the trick. But when using the ItemsSource2D Property to bind it to a 2-dimensional Array this does not work anymore.
Does anybody have a suggestion?
I found the problem: I am using the Alternative Style which the DataGrid2D Provides. There we already have this definiton:
<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}" >
<Setter Property="TextBlock.TextAlignment" Value="Center" />
...
If I change it here it works. I will have to rewrite the class a bit, for me to set the Alignment in XAML instead of with a style.
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>
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.