使用ListView上的复选框选择/取消全选(Windows 10)

时间:2017-07-05 12:12:15

标签: c# xaml listview uwp

您好我想要选中/取消选择带复选框的项目,我的源代码:

XAML:

<ListView
        x:Name="downListView"
        Grid.RowSpan="2"
        Margin="-1,29,1,3"
        ScrollViewer.HorizontalScrollBarVisibility="Hidden"
        ScrollViewer.HorizontalScrollMode="Disabled"
        ScrollViewer.VerticalScrollBarVisibility="Visible"
        ScrollViewer.VerticalScrollMode="Enabled"
        SelectionMode="Multiple">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid Margin="0,2,0,2" Loaded="downListView_Loaded">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <StackPanel Grid.Row="0" Orientation="Horizontal">
                    <TextBlock
                            x:Name="downPosition"
                            Foreground="#C2C2CA"
                            Text="{Binding position}" />
                    <TextBlock Text=":" />
                    <TextBlock
                            x:Name="downTitle"
                            Foreground="#C2C2CA"
                            Text="{Binding title}" />
                </StackPanel>
                <ProgressBar
                            x:Name="downBar"
                            Grid.Row="1"
                            Background="White"
                            Foreground="#DC143C"
                            Value="{Binding Percent}" />
                <TextBlock
                            x:Name="downStat"
                            Grid.Row="2"
                            Foreground="#C2C2CA"
                            Text="{Binding Status}" />
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

C#:

private ObservableCollection<VideoDatas> listItems = ...;
downListView.ItemsSource = listItems;

我试过了:

private void downListView_Loaded(object sender, RoutedEventArgs e)
{
    Panel panel = sender as Panel;
    if (panel != null)
    {
        ListViewItem lvi = FindParent<ListViewItem>(panel);
        if (lvi != null)
        {
            lvi.SetBinding(ListViewItem.IsSelectedProperty, new Binding()
            {
                Path = new PropertyPath(nameof(VideoDatas.IsSelected)),
                Source = panel.DataContext,
                Mode = BindingMode.TwoWay
            });
        }
    }
}
public static T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject
{
    var parent = VisualTreeHelper.GetParent(dependencyObject);

    if (parent == null)
        return null;

    var parentT = parent as T;
    return parentT ?? FindParent<T>(parent);
}

private void CheckedChanged()
{
    if (listItems != null)
    {
        foreach (VideoDatas item in listItems)
        {
            if (dSelectAll.IsChecked == true)
            {
                item.IsSelected = true;
                dSelectAllText.Text = "DeSelect All | Choose for download";
            }
            else
            {
                item.IsSelected = false;
                dSelectAllText.Text = "Select All | Choose for download";
            }
        }
    }
}

private void dSelectAll_Click(object sender, RoutedEventArgs e)
{
    CheckedChanged();
}

VideoDatas.cs

private bool isSelected;
public bool IsSelected
{
    get
    {
        return isSelected;
    }
    set
    {
        if (value != isSelected)
        {
            isSelected = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("IsSelected"));
            }
        }
    }
}

但是这段代码只选择ex:10项/ 170项。只需在listview上选择showin项目,不要选择其他项目

1 个答案:

答案 0 :(得分:0)

您正在手动勾选每个CheckBox内置的ListViewItem。这个解决方案看起来不错,但问题是ListView正在虚拟化它的项目,所以它只能实现其中一些来节省内存,而你最终只能勾选它们。

正确的方法是使用内置方法downListView.SelectAll()来选择所有这些方法,并downListView.SelectedItems.Clear();取消选择所有方法。