问题
Imagine I have a CheckBox custom style named "MyCheckBoxStyle".
How can I make a Datagrid style that embeds a custom DataGridCheckBoxColumn style based on my MyCheckBoxStyle?
回答1:
You can use DataGridTemplateColumn to create a custom checkboxcolumn
<Custom:DataGridTemplateColumn x:Name="gdchk" Header="Test" MaxWidth="50">
<Custom:DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<CheckBox IsChecked="{Binding Path = classname}" HorizontalAlignment="Center" Style="{DynamicResource myCheckBoxStyle}"/>
</DataTemplate>
</Custom:DataGridTemplateColumn.CellTemplate>
</Custom:DataGridTemplateColumn>
Hope this helps.
回答2:
You can simply use your defined style with ElementStyle attribute.
Style defined in resources:
<Style x:Key="MyCheckBoxStyle" TargetType="{x:Type CheckBox}"> ... </Style>
And my datagrid checkbox column:
<DataGridCheckBoxColumn ElementStyle="{StaticResource MyCheckBoxStyle}" Binding="{Binding someValue}" />
回答3:
try this
<DataGridCheckBoxColumn MinWidth="100"
Binding="{Binding Path=BoolValue}"
Header="Bool Column"
IsThreeState="True">
<DataGridCheckBoxColumn.ElementStyle>
<Style TargetType="CheckBox">
<Setter Property="Background" Value="{Binding BoolValueColour, Converter={StaticResource MyConverter}}" />
</Style>
</DataGridCheckBoxColumn.ElementStyle>
</DataGridCheckBoxColumn>
回答4:
For me did not work until I've found the following workaround:
<DataGridTemplateColumn
Header="Skip" Width="18"
IsReadOnly="False" CanUserResize="False">
<DataGridTemplateColumn.CellTemplate >
<DataTemplate DataType="gfxModule:BatchPairItemModel">
<CheckBox
Tag="{Binding}"
IsChecked="{Binding Tag.Skip, Mode=TwoWay,
RelativeSource={RelativeSource Self}}"
Template="{DynamicResource CheckBoxCircleXTemplate}"
ToolTip="Skip"
/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
来源:https://stackoverflow.com/questions/10721880/wpf-datagridcheckboxcolumn-style