如何在WPF数据网格列中制作互斥的复选框

4

我有以下的DataGrid代码:

<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>

这个用户控件 Grid 由三列组成,这些列应该是互斥的。我希望通过在 XAML 中创建触发器来实现这一点,我们该怎么做?


你遇到的问题是明确设置一个值会覆盖任何触发器值。由于你正在进行数据绑定,所以你不能在后端模型对象中实现排他性吗? - Bob Vale
你为什么要这样做?这是应用程序逻辑的一部分,应该放在 ViewModel 中而不是 View 中。你真的不应该使用 XAML 中的触发器来实现应用程序逻辑/约束。 - Jaster
由于这些复选框无法从相应的C#文件中访问,我们如何使用C#代码实现它? - Abhi
2个回答

4

您可以使用类似复选框的单选按钮来实现互斥行为。如果在绑定单选按钮时遇到任何问题:单选按钮绑定

<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中是更加合适的位置。


谢谢您的建议。无样式控件使得轻松更换样式并实现预期行为成为可能,而不管后端使用了什么实现方式。我根据您的建议添加了所需的样式 - 完美地工作! - SliverNinja - MSFT

0

我正在尝试为第三列"InFor"创建这样的样式,但根本不起作用。

<Style x:Key="mutualCheckInForLoop" TargetType="{x:Type WPFtoolkit:DataGridCell}" BasedOn="{StaticResource {x:Type WPFtoolkit:DataGridCell}}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=dgChkRepeatingData,Path=IsChecked}" Value="True">
                    <Setter Property="CheckBox.IsChecked" Value="False" />
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=dgChkMaxElement,Path=IsChecked}" Value="True">
                    <Setter Property="CheckBox.IsChecked" Value="False" />
                </DataTrigger>
            </Style.Triggers>
        </Style>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接