wpf datagridcheckboxcolumn style

跟風遠走 提交于 2019-12-23 17:02:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!