【问题标题】:Select/DeSelect All with Checkbox on ListView (Windows 10)使用 ListView 上的复选框全选/取消全选 (Windows 10)
【发布时间】:2023-03-18 06:15:01
【问题描述】:

嗨,我想用复选框选择/取消选择项目,我的源代码:

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 个项目。只需在列表视图中选择显示项目,不要选择其他项目

【问题讨论】:

    标签: c# xaml listview uwp


    【解决方案1】:

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

    正确的做法是使用内置方法downListView.SelectAll()全选,downListView.SelectedItems.Clear();取消全选。

    【讨论】:

    • downListView.SelectedItems.Clear(); 怎么样?它的工作原理相同,但更具可读性。有什么理由不用它吗?
    • @MarianDolinský 哈哈,你把我带到了那里。我想我只是没有正确思考大声笑。
    • 所以有更多的方法:)我首先尝试了这段代码,然后尝试了 SelectRange 和 DeselectRange :)。
    猜你喜欢
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多