S
S
Sergey Vinogradov2015-07-07 17:57:26
WPF
Sergey Vinogradov, 2015-07-07 17:57:26

How to remove flickering CheckBoxes when updating data in wpf?

There is a ListBox with data. The data is updated, added about 1 time per second. The data at each update must be sorted by a certain parameter. All data rows have a CheckBox to indicate if they need to be updated further. In the case when the sorted data in the ListBox is updated, all CheckBox's are redrawn and the checkboxes marked with a checkbox flicker.
As follows:

<ListBox x:Name="lstbx" ItemsSource="{Binding Data.IDs}" SelectionMode="Single" SelectedItem="{Binding selectedID}"
HorizontalAlignment="Left" MinHeight="305" Margin="5" VerticalAlignment="Top" MinWidth="169" FontSize="10">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid Margin="0,2">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="20"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <CheckBox IsChecked="{Binding Enable}" VerticalAlignment="Center"/>
                <TextBlock VerticalAlignment="Stretch" Grid.Column="1">
                    <TextBlock.Text>
                        <MultiBinding StringFormat="{}{0}: {1}">
                            <Binding Path="Name"/>
                            <Binding Path="DiffCount"/>
                        </MultiBinding>
                    </TextBlock.Text>
                </TextBlock>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Demonstration of the problem: Video file in .AVI (196 kb) The
question is, accordingly, how to avoid such a problem as flickering in this case?
And, after all, what is the best way to sort the data in the ListBox?
Initially, each time IDs[i].DiffCount changed, it called the PropertyChanged event, and set sorting in the event handler:
lstbx.Items.SortDescriptions.Clear();
lstbx.Items.SortDescriptions.Add(new System.ComponentModel.SortDescription("DiffCount", System.ComponentModel.ListSortDirection.Descending));

But somehow I didn’t like this result and I changed it to the option of creating a new collection sorted from the original one, after which I simply replaced the old one with the new one:
var idTemp = IDs.OrderByDescending(i => i.DiffCount).Select(i => i);
var newIDs = new Collection();
foreach (var id in idTemp)
    newIDs.Add(id);
IDs = newIDs;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Larry Underwood, 2015-07-07
@Hydro

About sorting:

<ListBox Loaded="ListBoxOnLoadedHandler">
</ListBox>

private void ListBoxOnLoadedHandler(object sender, RoutedEventArgs e)
      {
        var listbox = sender as ListBox;
        if (listbox == null)
          return;
        var view = CollectionViewSource.GetDefaultView(listbox.ItemsSource);
        view.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
      }

What does flickering mean? Do the ticks flicker? or flickering focus? can you describe in more detail.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question