I have a user control with defined resources.
Here is some code for illustration.
<UserControl.Resources>
<SolidColorBrush x:Key="foregroundColor" Color="Red"/>
<Style x:Key="buttonFontIconStyle" TargetType="FontIcon">
<Setter Property="FontFamily" Value="Segoe MDL2 Assets"></Setter>
<Setter Property="Foreground" Value="{Binding ???}"></Setter>
</Style>
<Style x:Key="menuItemLabelStyle" TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center"></Setter>
<Setter Property="Foreground" Value="{Binding ???}"></Setter>
</Style>
</UserControl.Resources>
Now, I wish to use value defined in foreground color for buttonFontIconStyle, menuItemLabelStyle (and many others). Is it somehow possible to bind to value from resources in resources, or is there a way to specifiy color once (in xaml preferrably) and use it in multiple resources styles?
You can use the StaticResource :
<Style x:Key="buttonFontIconStyle" TargetType="FontIcon">
<Setter Property="FontFamily" Value="Segoe MDL2 Assets"></Setter>
<Setter Property="Foreground" Value="{StaticResource foregroundColor}"></Setter>
</Style>
<Style x:Key="menuItemLabelStyle" TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center"></Setter>
<Setter Property="Foreground" Value="{StaticResource foregroundColor}"></Setter>
</Style>
Related
(Note: The Snipping Tool has hidden the mouse cursor hovering over Amelia Earhart in this screen grab.)
When the mouse is RowHeader.IsMouseOver=True then the style is triggered and in this example the font is set to Bold and Italic and the colours change.
Problem: I want to call the same trigger when I Click or Mouseover on any DataGridCell in the row. So if I click on the cell "Hello World" then the RowHeader "Amelia Earhart" is formatted.
<DataGrid.RowHeaderStyle>
<Style TargetType="DataGridRowHeader">
<Setter Property="Width" Value="25"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<!-- I want to trigger this IsMouseOver from DataGridCell.IsMouseOver -->
<Label Content="{Binding [0]}" FontSize="10" Margin="0" >
<Label.Style>
<Style x:Name="sOne" TargetType="Label">
<Setter Property="Foreground" Value="WhiteSmoke"/>
<Setter Property="Background" Value="CadetBlue"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="FontStyle" Value="Italic" />
<Setter Property="Foreground" Value="Cyan"/>
<Setter Property="Background" Value="Navy"/>
</Trigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.RowHeaderStyle>
I have xaml styles that have different target types but are otherwise identical. Is there a way I could cut out the duplication and define the style only once?
<Style TargetType="TextBlock">
<Setter Property="Height" Value="{StaticResource ElementHeight}"/>
<Setter Property="MinWidth" Value="{StaticResource ElementMinWidth}"/>
<Setter Property="Margin" Value="{StaticResource ElementMargin}"/>
</Style>
<Style TargetType="TextBox">
<Setter Property="Height" Value="{StaticResource ElementHeight}"/>
<Setter Property="MinWidth" Value="{StaticResource ElementMinWidth}"/>
<Setter Property="Margin" Value="{StaticResource ElementMargin}"/>
</Style>
<Style TargetType="ComboBox">
<Setter Property="Height" Value="{StaticResource ElementHeight}"/>
<Setter Property="MinWidth" Value="{StaticResource ElementMinWidth}"/>
<Setter Property="Margin" Value="{StaticResource ElementMargin}"/>
</Style>
You could use style inheritance with the help of Style.BasedOn.
First define the base style:
<Style x:Key="BaseStyle" TargetType="FrameworkElement">
<Setter Property="Height" Value="80"/>
<Setter Property="MinWidth" Value="80"/>
<Setter Property="Margin" Value="80"/>
</Style>
Then "inherit" styles from that for the controls you want:
<Style TargetType="TextBlock" BasedOn="{StaticResource BaseStyle}"/>
<Style TargetType="TextBox" BasedOn="{StaticResource BaseStyle}"/>
<Style TargetType="ComboBox" BasedOn="{StaticResource BaseStyle}"/>
How to use multiple key(used in ResourceDictionary) in Style="{StaticResource
TopHeader }"
You can combile your multiple styles inside new created style.
For example:
<Style x:Key="Style1" TargetType="Button">
<Setter Property="Background" Value="Yellow" />
</Style>
<Style x:Key="Style2" TargetType="Button">
<Setter Property="Foreground" Value="Red" />
</Style>
<Style x:Key="Style1Style2" TargetType="Button">
<Setter Property="Background" Value="Yellow" />
<Setter Property="Foreground" Value="Red" />
</Style>
or you can create BasedOn style:
<Style x:Key="Style3" TargetType="Button" BasedOn="{StaticResource Style2}">
<Setter Property="Background" Value="Yellow" />
</Style>
Is it possible to set Foreground property from the Style? Looking like it has no effect.
<Style x:Key="MyPageNameStyle" TargetType="TextBlock">
<Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}" />
<Setter Property="Margin" Value="0,12,0,0"/>
<Setter Property="Foreground" Value="Green"/>
</Style>
It's simple be sure you bind it to a static resource
<Grid.Resources>
<Style x:Key="MyPageNameStyle" TargetType="TextBlock">
<Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}" />
<Setter Property="Margin" Value="0,12,0,0"/>
<Setter Property="Foreground" Value="Green"/>
</Style>
</Grid.Resources>
<TextBlock Style="{StaticResource MyPageNameStyle}" Text="WP8 Demodccxzcxzczsczczxcxzczczczcz" Margin="9,-7,0,0" />
Youll be able to see the effect in the xaml designer only
I have a datagrid which auto generates the columns.
<DataGrid Name="QueryGrid" AutoGenerateColumns="True" Height="1000" Width="1135" ItemsSource="{Binding QueryTable}" Visibility="{Binding Path=QueryGridVisiblity, Converter={StaticResource BoolToVis}}" />
I have to make the column names bold. How do i do this? Any suggestions?
Regards,
Sagar
Here is the answer i have found:
<DataGrid Name="QueryGrid" AutoGenerateColumns="True" Height="900" Width="1135" ItemsSource="{Binding QueryTable}" Visibility="{Binding Path=QueryGridVisiblity, Converter={StaticResource BoolToVis}}">
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</DataGrid.ColumnHeaderStyle>
</DataGrid>
<DataGridTextColumn.HeaderStyle>
<Style
TargetType="DataGridColumnHeader">
<Setter
Property="Background"
Value="SteelBlue"
/>
<Setter
Property="HorizontalContentAlignment"
Value="Center"
/>
<Setter Property="FontSize" Value="17"/>
<Setter Property="FontWeight" Value="Bold"/>
</Style>
</DataGridTextColumn.HeaderStyle>