多选列表视图,选中全部复选框。关于绑定财产的Datatrigger

时间:2017-02-14 21:40:28

标签: c# wpf binding datatrigger selectall

我有一个包含两列的网格视图的列表视图。第一列包含绑定到listviewitem selected属性的复选框,第二列是文本。在复选框列的标题中,我有一个复选框,我想用作选择/取消选择所有按钮。我使用了数据触发器来执行此操作,但只有当我删除复选框和所选属性之间的绑定时它才有效。我是否应该能够使用数据触发器来设置绑定属性?

Screenshot

代码隐藏

namespace ListviewWCheckboxes
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        List<string> listItems = new List<string>() { "foo", "bar", "blah" };
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            pdflistView.ItemsSource = listItems;
        }
    }
}

XAML

<Window x:Class="ListviewWCheckboxes.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ListviewWCheckboxes"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525"
        Loaded="Window_Loaded">
    <Grid>
        <ListView x:Name="pdflistView" HorizontalAlignment="Left" Height="300" Margin="5" VerticalAlignment="Top" Width="240"
                      SelectionMode="Extended"
                      >
            <ListView.View>
                <GridView>
                    <GridViewColumn>
                        <GridViewColumn.Header>
                            <CheckBox x:Name="ckbxSelectAll"/>
                        </GridViewColumn.Header>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox IsChecked="{Binding Path=IsSelected, 
                                                RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}, Mode=FindAncestor},Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
                                    <CheckBox.Style>
                                        <Style TargetType="CheckBox">
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding ElementName=ckbxSelectAll, Path=IsChecked}" Value="True">
                                                    <Setter Property="IsChecked" Value="True" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding ElementName=ckbxSelectAll, Path=IsChecked}" Value="False">
                                                    <Setter Property="IsChecked" Value="False" />
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </CheckBox.Style>

                                </CheckBox>
                                <DataTemplate.Triggers>

                                </DataTemplate.Triggers>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>

                    </GridViewColumn>
                    <GridViewColumn Header="Pdf" DisplayMemberBinding="{Binding}"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

1 个答案:

答案 0 :(得分:0)

我认为实现这一点的最佳方法是在类上创建一个属性“IsSelected”,并将其绑定到复选框(IsChecked)和listview(isSelected)。处理标题复选框的已检查事件,并手动切换集合中项目的IsSelected属性,以便更新checkbox和listviewitems。如果您有任何其他问题,请告诉我