gpt4 book ai didi

c# - 如何创建三态 CheckBox 来汇总 ListView 中所有 CheckBox 的状态?

转载 作者:太空宇宙 更新时间:2023-11-03 14:17:55 25 4
gpt4 key购买 nike

我有一个 WPF ListView,其中包含 CheckBox 列和自定义对象的其他列。我想在这个 ListView 上放置另一个 CheckBox——三态 CheckBox——所以它的状态将绑定(bind)到选中的复选框。如果每个人都被选中,则上部复选框的状态将被选中。如果只有其中一些,状态将是不确定的。否则它的状态将被取消检查。 (就像在 Gmail 中一样)

最佳答案

我找到了一些解决方案

<StackPanel>
<CheckBox Name="chbxAll" Checked="chbxAll_Checked" Unchecked="chbxAll_Unchecked" Indeterminate="chbxAll_Indeterminate" IsThreeState="True" >Select All</CheckBox>
<ListView Name="lstFoundedFiles" SelectionChanged="lstFoundedFiles_SelectionChanged" SelectionMode="Multiple" ItemsSource="{Binding Files}">
<ListView.Resources>
<Style TargetType="ListViewItem">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Aquamarine"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn Width="50" Header="Check">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox x:Name="chbxItem" IsChecked="{Binding Path=IsSelected,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="File">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" ></TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Location">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path}"></TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</StackPanel>

============================================= ===========================

代码隐藏:

// True if we should ignore check change events.
private bool IgnoreCheckChangeEvents = false;

private void lstFoundedFiles_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (IgnoreCheckChangeEvents) return;

int temp = lstFoundedFiles.SelectedItems.Count;
IgnoreCheckChangeEvents = true;
if (temp == lstFoundedFiles.Items.Count)
{
chbxAll.IsChecked = true;
}
else if (temp == 0)
{
chbxAll.IsChecked = false;
}
else
{
chbxAll.IsChecked = null;
}

IgnoreCheckChangeEvents = false;
}

private void chbxAll_Checked(object sender, RoutedEventArgs e)
{
if (IgnoreCheckChangeEvents) return;

IgnoreCheckChangeEvents = true;

lstFoundedFiles.SelectAll();

IgnoreCheckChangeEvents = false;
}

private void chbxAll_Unchecked(object sender, RoutedEventArgs e)
{
if (IgnoreCheckChangeEvents) return;

IgnoreCheckChangeEvents = true;

lstFoundedFiles.UnselectAll();

IgnoreCheckChangeEvents = false;
}

private void chbxAll_Indeterminate(object sender, RoutedEventArgs e)
{
if (IgnoreCheckChangeEvents) return;

chbxAll.IsChecked = false;

IgnoreCheckChangeEvents = true;

lstFoundedFiles.UnselectAll();

IgnoreCheckChangeEvents = false;
}

关于c# - 如何创建三态 CheckBox 来汇总 ListView 中所有 CheckBox 的状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6175516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com