WPF:如何避免ListBox或ListView中的复选框闪烁?
如何避免WPF列表框或列表视图中的复选框闪烁?通过单击刷新按钮或滚动列表框,可以使用下面的代码进行复制。如果IsChecked为false,则不会闪烁 Window1.xaml:WPF:如何避免ListBox或ListView中的复选框闪烁?,wpf,rendering,Wpf,Rendering,如何避免WPF列表框或列表视图中的复选框闪烁?通过单击刷新按钮或滚动列表框,可以使用下面的代码进行复制。如果IsChecked为false,则不会闪烁 Window1.xaml: <Window x:Class="WpfApplication6.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft
<Window x:Class="WpfApplication6.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ListBox Name="listBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="True"
VerticalAlignment="Center"/>
<Label Padding="3"
Content="{Binding}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="Refresh"
Grid.Column="1"
VerticalAlignment="Top"
Click="Button_Click"/>
</Grid>
</Window>
Window1.xaml.cs:
using System.Windows;
namespace WpfApplication6
{
partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Button_Click(null, null);
}
void Button_Click(object sender, RoutedEventArgs e)
{
var items = new int[10000];
for (int i = 0; i < items.Length; i++)
items[i] = i + 1;
listBox.ItemsSource = items;
}
}
}
使用System.Windows;
命名空间WpfApplication6
{
部分类Window1:Window
{
公共窗口1()
{
初始化组件();
按钮点击(空,空);
}
无效按钮\单击(对象发送器,路由目标)
{
var项目=新整数[10000];
对于(int i=0;i
您是说复选框被选中了吗?我认为在选中/设置复选框时需要更改动画
它不会出现在Windows XP上(这就是为什么我认为它是一个动画),我还没有测试Vista:)
祝你好运。它会闪烁,因为你正在扔掉旧项目资源并创建一个新项目。这需要重做所有绑定,并且需要重新创建显示每个项的模板。为了避免重新创建整个列表的性能开销,只需修改现有ItemsSource中的各个元素。然后,绑定到更改的属性和/或项目的DataTemplate部分将自动更新,而无需重新创建整个列表视图。这样做将消除您看到的“闪烁” 请尝试以下代码隐藏:
public partial class MainWindow : Window
{
private ObservableCollection<object> _items;
public MainWindow()
{
InitializeComponent();
_items = new ObservableCollection<object>();
for (int i = 0; i < 10000; i++)
_items.Add(i + 1);
listBox.ItemsSource = _items;
}
void Button_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < _items.Count;i++)
{
if (!(_items[i] is int)) continue;
_items[i] = (int)_items[i] + 1;
}
}
}
公共部分类主窗口:窗口
{
私人可观测收集项目;
公共主窗口()
{
初始化组件();
_items=新的ObservableCollection();
对于(int i=0;i<10000;i++)
_增加(i+1);
listBox.ItemsSource=\u项;
}
无效按钮\单击(对象发送器,路由目标)
{
对于(int i=0;i<\u items.Count;i++)
{
如果(!(_items[i]为int))继续;
_项目[i]=(int)_项目[i]+1;
}
}
}
+1致有正确答案的Snake
除此之外:
复选框控件有一个带有情节提要动画的控件模板,该模板在选中状态更改时为选中图标的打开/关闭设置动画。当您绑定到ObservableCollection并重新创建ItemsSource时,选中状态会发生变化,这将导致创建新的复选框(IsChecked=false)并绑定到ViewModel(这可能会导致IsChecked=True)
要禁用此“功能”,您可以修改填写ObservableCollection的方式,或者如果不可能,您可以更改复选框的模板/样式
只需对Checkbox的ControlTemplate进行反向工程(使用blend,或使用一个WPF主题)并找到这些行。您需要将两个动画的持续时间设置为零
<!-- Inside the CheckBoxTemplate ControlTemplate -->
<Storyboard x:Key="CheckedTrue">
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="CheckIcon"
Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="1" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="CheckedFalse">
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="CheckIcon"
Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.4000000" Value="0" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>