如何检查带触发器的复选框?

时间:2021-03-02 08:40:58

My TreeView nodes have a checkbox. If the User checks one checkbox, all other checkboxes from the rootnode subnodecollection should automatically be disabled.

我的TreeView节点有一个复选框。如果用户选中一个复选框,则应自动禁用rootnode subnodecollection中的所有其他复选框。

Here is my treeview:

这是我的树视图:

<TreeView ItemsSource="{Binding Items}">
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}" >
            <Style.Triggers>
                <Trigger Property="HasItems" Value="true">
                    <Setter Property="Focusable" Value="False"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </TreeView.ItemContainerStyle>
        <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding SubNodes}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="20"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <CheckBox Grid.Column="0"/>
                <TextBlock Text="{Binding Text}" Grid.Column="1"/>
            </Grid>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

2 个解决方案

#1


0  

Modifying the ViewModel is the way to go:

修改ViewModel是要走的路:

Each item in the SubNodes collection should implement a new bool property, 'Enabled' for example and another bool 'Checked'. Then bind 'Checked' to the 'IsChecked' property of the Checkbox and 'Enabled' to 'IsEnabled' and build the necessary logic in the set accesor of 'Checked' to set to false the 'Enabled' property in every other item of the collection.

SubNodes集合中的每个项目都应该实现一个新的bool属性,例如'Enabled'和另一个bool'Checked'。然后将'Checked'绑定到Checkbox的'IsChecked'属性并'Enabled'绑定到'IsEnabled'并在'Checked'的set访问器中构建必要的逻辑,将其设置为false,其中每个其他项中的'Enabled'属性采集。

#2


0  

You can derive from RadioBox and implement this feature on your own or your view model registers for changes of the checked property and resets all other list items checked property.

您可以从RadioBox派生并在您自己或您的视图模型寄存器上实现此功能,以更改已检查属性并重置所有其他列表项检查属性。

#1


0  

Modifying the ViewModel is the way to go:

修改ViewModel是要走的路:

Each item in the SubNodes collection should implement a new bool property, 'Enabled' for example and another bool 'Checked'. Then bind 'Checked' to the 'IsChecked' property of the Checkbox and 'Enabled' to 'IsEnabled' and build the necessary logic in the set accesor of 'Checked' to set to false the 'Enabled' property in every other item of the collection.

SubNodes集合中的每个项目都应该实现一个新的bool属性,例如'Enabled'和另一个bool'Checked'。然后将'Checked'绑定到Checkbox的'IsChecked'属性并'Enabled'绑定到'IsEnabled'并在'Checked'的set访问器中构建必要的逻辑,将其设置为false,其中每个其他项中的'Enabled'属性采集。

#2


0  

You can derive from RadioBox and implement this feature on your own or your view model registers for changes of the checked property and resets all other list items checked property.

您可以从RadioBox派生并在您自己或您的视图模型寄存器上实现此功能,以更改已检查属性并重置所有其他列表项检查属性。