2

Hi i want do Select/DeSelect items with checkbox , my source codes :

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;

I tried this :

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"));
            }
        }
    }
}

But this code just select ex:10 items/170 items.Just select showin items on listview, dont select other items

0

1 Answer 1

1

You are manually ticking the built-in CheckBox inside each ListViewItem on load. This solution looks OK but the issue is that the ListView is virtualizing its items so it only realizes a few of them to save memory, and you end up only ticking them.

The right way is to use the built-in methods downListView.SelectAll() to select all of them, and downListView.SelectedItems.Clear(); to deselect all of them.

Sign up to request clarification or add additional context in comments.

3 Comments

What about downListView.SelectedItems.Clear();? It works the same but is more readable. Is there any reason why not to use it?
@MarianDolinský haha you got me there. I think I just wasn't thinking properly lol.
so more ways to do :) i first tried this code ,after tried SelectRange and DeselectRange :) .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.