c# - 带有复选框的 Wpf TreeView
问题描述
我需要将“全选”添加到我的 wpf 应用程序中,我尝试做一个简单的绑定,但它不起作用。
有人可以建议,执行简单的绑定或其他严格的解决方案吗?
这是我的树视图 xaml :
<TreeView Name="CurrentTreeView" Margin="5" BorderBrush="Transparent" BorderThickness="0"
ScrollViewer.VerticalScrollBarVisibility="Auto">
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
<Setter Property="IsExpanded" Value="{Binding IsExpanded}" />
<Setter Property="HeaderTemplate" >
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox
Focusable="False"
Checked="CheckedItemCurreuntSession"
Unchecked="UnCheckedItemCurreuntSession"
Name="treeChk"
Tag="{Binding Path=ChName}"
Style="{StaticResource CheckBoxStyle1}"
IsChecked="{Binding Path=IsChecked}"
VerticalAlignment="Center"/>
<TextBlock VerticalAlignment="Center" Text="{Binding}"
Margin="5"
TextOptions.TextFormattingMode="Display"
TextOptions.TextHintingMode="Auto"
/>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
解决方案
<Style x:Key="CheckBoxListStyle" TargetType="{x:Type ListBox}">
<Setter Property="SelectionMode" Value="Multiple"/>
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Margin" Value="2"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<CheckBox Focusable="False" IsChecked="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent} }">
<ContentPresenter/>
</CheckBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
推荐阅读
- python - 从 Kaggle 的训练集生成验证集
- arrays - Powershell 批量删除-ADGroupMember
- python - 抓取 API
- .net - 在 VS Code 中找不到用于调试 dotnet core api 的 ProjectName.dll 附加过程?
- sql - ORA-01722: 无效号码 01722. 00000 - “无效号码”
- sql-server - SQL 使用存储过程组合垂直表中的值
- xslt - 使用 xslt 在现有 xml 节点下追加新的 xml 节点
- xpages - XPage 无法正确呈现
- java - Tomcat中奇怪的JSON字符串失真
- html - CSS不能居中菜单和子菜单2nt行中的空白