本文介绍了如何在WPF DataGrid列中进行互相排斥的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下DataGrid代码

I am having following DataGrid Code

<UserControl x:Class="abc.WPFApp.UCGrid"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WPFtoolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
    xmlns:local="clr-namespace:abc.WPFApp">

    <UserControl.Resources>
<!--Restrict editing based on IsVariable-->
        <Style x:Key="CheckBoxCellStyle" TargetType="{x:Type CheckBox}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsVariable}" Value="true">
                    <Setter Property="IsEnabled" Value="false"/>
                </DataTrigger>
            </Style.Triggers>
            <Setter Property="HorizontalAlignment" Value="Center"/>
        </Style>
</UserControl.Resources>
<Grid>
        <WPFtoolkit:DataGrid x:Name="UCdataGridView" ItemsSource="{Binding}"
                                                     CellStyle="{StaticResource defaultCellStyle}"
                                                     RowStyle="{StaticResource defaultRowStyle}"
                                                     ColumnHeaderStyle="{StaticResource defaultDataGridColumnHeader}"
                                                     SelectionUnit="FullRow"
                                                     IsSynchronizedWithCurrentItem="True" 
                                                     RowBackground="White" 
                                                     AlternatingRowBackground="AliceBlue"
                                     AutoGenerateColumns="False" SelectionMode="Extended" RowHeaderWidth="20"
                                     CanUserAddRows="True" CanUserDeleteRows="True" CanUserReorderColumns="False" 
                                     CanUserResizeColumns="True" AllowDrop="True" KeyUp="UCGridKeyUp" >
            <WPFtoolkit:DataGrid.Columns>


<WPFtoolkit:DataGridCheckBoxColumn x:Name="dgChkRepeatingData" Binding="{Binding Path=MasterDataFlag}" MaxWidth="135" MinWidth="80"
                                     Header="Repeating data" Visibility="Collapsed" IsReadOnly="{Binding (IsVariable)}" 
                                     EditingElementStyle="{StaticResource CheckBoxCellStyle}"
                                      >
                </WPFtoolkit:DataGridCheckBoxColumn>

                <WPFtoolkit:DataGridCheckBoxColumn MaxWidth="100" Header="Max Element" x:Name="dgChkMaxElement"
                                                   Binding="{Binding Path=MaxElement}" MinWidth="70" Visibility="Collapsed" 
                                    EditingElementStyle="{StaticResource CheckBoxCellStyle}">
                </WPFtoolkit:DataGridCheckBoxColumn>

                <WPFtoolkit:DataGridCheckBoxColumn MaxWidth="100" Header="In For Loop" x:Name="dgChkInForLoop"
                                                   Binding="{Binding Path=InForLoop}" MinWidth="70" Visibility="Collapsed" 
                                    EditingElementStyle="{StaticResource CheckBoxCellStyle}">
                </WPFtoolkit:DataGridCheckBoxColumn>

                <WPFtoolkit:DataGridTextColumn x:Name="dgXPath" Binding="{Binding Path=XPath}" Header="XPath" Width="500"
                                               Visibility="Collapsed" IsReadOnly="{Binding Path=IsVariable}"
                                               EditingElementStyle="{StaticResource TextBoxCellStyle}"/>
</WPFtoolkit:DataGrid.Columns>
        </WPFtoolkit:DataGrid>

此用户控件网格包含三列应互为排他的列。
我想通过在XAML本身创建触发器来实现这一点,我们该怎么做?

This user control Grid comprised if three Columns which should be mutually exclusive.I want to achieve this by creating Triggers in XAML itself , How can we do this ?

推荐答案

具有复选框样式的单选按钮实现互斥行为。如果绑定单选按钮有任何问题:

You can use radiobuttons with a checkbox style to achieve mutually exclusive behavior. If you have any problem binding radiobuttons: radiobutton-binding

<RadioButton IsChecked="{Binding VMProperty}" GroupName="IsSelectedGroup">
    <RadioButton.Style>
        <Style TargetType="{x:Type RadioButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="RadioButton">
                        <CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay, RelativeSource={RelativeSource AncestorType=RadioButton}}" Content="{TemplateBinding Content}" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </RadioButton.Style>
</RadioButton>

但是,我同意Jaster。如果您将每行绑定到ViewModel,则放置此逻辑限制的位置为ViewModel。

But, i agree with Jaster. If you have each row binded to a ViewModel, the place to put this logic restriction is the ViewModel.

这篇关于如何在WPF DataGrid列中进行互相排斥的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 01:34