WPF: How can I avoid the flickering of the checked checkboxes in a ListBox or a ListView?

后端 未结 3 1772
栀梦
栀梦 2020-12-20 18:34

How can I avoid the flickering of the checked checkboxes in a WPF ListBox or ListView ? It can be reproduced with the code below by clicking on the Refresh button or by scro

3条回答
  •  无人及你
    2020-12-20 19:03

    +1 to Snake who has the right answer here.

    To add to this:

    The CheckBox control has a control template with storyboard animation which animates the checked icon on/off when the checked state changes. The Checked state changes as you are binding to ObservableCollection and recreating the ItemsSource which is causing new Checkboxes to be created (with IsChecked=false) and bound to your ViewModel (which probably results in IsChecked=True).

    To disable this 'feature' you can either modify how you fill an ObservableCollection, or if that's not possible, you can change the template / style of the Checkbox.

    Just reverse engineer the ControlTemplate of Checkbox (using blend, or by using one of the WPF Themes) and find these lines. you need to set the duration of the two animations to zero

    
    
        
            
        
    
    
        
            
        
    
    

提交回复
热议问题