开发者

How can i check checkboxes with a trigger?

My TreeView nodes have a checkbox. If the User checks one checkbox, all other checkboxes from the rootnode subnodecollection开发者_如何学运维 should automatically be disabled.

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>


Modifying the ViewModel is the way to go:

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜