Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
swdev:dotnet:wpf:styles_and_triggers [2010/12/17 16:05]
smayr [TreeView with new Template]
swdev:dotnet:wpf:styles_and_triggers [2011/04/27 14:30] (current)
smayr
Line 41: Line 41:
 Source: [[http://www.microsoft.com/belux/msdn/nl/community/columns/gillcleeren/wpf_stylesandtriggers.mspx|Styles and Triggers in WPF]] Source: [[http://www.microsoft.com/belux/msdn/nl/community/columns/gillcleeren/wpf_stylesandtriggers.mspx|Styles and Triggers in WPF]]
  
 +=== Style Tricks ===
 +
 +To use a parent property value, rather than a predefined value, use ''TemplateBinding'' + //PropertyName//. For example:
 +<code xml>
 +<Style...>
 +  <Setter Property="Template">
 +    <Setter.Value>
 +       <ControlTemplate TargetType="{x:Type ListBox}">
 +           <Border 
 +               HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
 +               Background="{TemplateBinding Background}" 
 +               />
 +               ...
 +       </ControlTemplate>
 +    </Setter.Value>
 +  </Setter>
 +</Style>
 +</code>
 +
 +Restyle Datagrid's Checkbox column:
 +<code xml>
 +<UserControl.Resources>
 +    <DataTemplate x:Key="MyCheckBoxTemplate">
 +        <CheckBox Style="{StaticResource AnyResourceKeyInApplecation}"/>
 +    </DataTemplate>
 +</UserControl.Resources>
 +<DataGrid x:Name="dataGrid" />
 +</code>
 +
 +<code csharp>
 +this.dataGrid.Columns.Add(new DataGridTemplateColumn{
 +    CellTemplate=this.Resources["MyCheckBoxTemplate"] as DataTemplate});
 +
 +// OR this way:    
 +column2.ElementStyle = Application.Current.FindResource("MyCheckBoxStyle");
 +</code>
 == Triggers == == Triggers ==
  
Line 139: Line 175:
         <ControlTemplate TargetType="{x:Type CheckBox}">         <ControlTemplate TargetType="{x:Type CheckBox}">
             <Viewbox>             <Viewbox>
-                <Border CornerRadius="20" BorderBrush="DarkGray" BorderThickness="3" Grid.ColumnSpan="3" Grid.RowSpan="3">+                <Border x:Name="borderMain" CornerRadius="20" BorderBrush="DarkGray" BorderThickness="3" Grid.ColumnSpan="3" Grid.RowSpan="3">
                     <Grid Width="191" Height="50">                     <Grid Width="191" Height="50">
                         <Grid.RowDefinitions>                         <Grid.RowDefinitions>
Line 172: Line 208:
             <ControlTemplate.Triggers>             <ControlTemplate.Triggers>
                 <Trigger Property="IsChecked" Value="true">                 <Trigger Property="IsChecked" Value="true">
-                    <Setter TargetName="btnCheckBox" Property="Grid.Column" Value="1" /> +                      <Setter TargetName="btnCheckBox" Property="Grid.Column" Value="1" /> 
-                    <Setter TargetName="txtText" Property="Grid.Column" Value="0" /> +                      <Setter TargetName="txtMain" Property="Grid.Column" Value="0" /> 
-                    <Setter TargetName="txtText" Property="Text" Value="On" />+                      <Setter TargetName="txtMain" Property="Text" Value="On" /> 
 +                      <Setter TargetName="borderMain" Property="Background" Value="SteelBlue" />
                 </Trigger>                 </Trigger>
             </ControlTemplate.Triggers>             </ControlTemplate.Triggers>
Line 181: Line 218:
 </CheckBox> </CheckBox>
 </code> </code>
 +
 +== Resources ==
 +  * [[http://msdn.microsoft.com/en-us/library/ms745683.aspx|MSDN: Styling and Templating]]