如何避免WPF ListBox或ListView中已选中复选框的闪烁?通过单击"刷新"按钮或滚动列表框,可以使用以下代码复制它.如果IsChecked为false,则不会闪烁.
Window1.xaml:
<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>
Run Code Online (Sandbox Code Playgroud)
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
它闪烁是因为您正在丢弃旧的 ItemsSource 并创建一个新的。这需要重做所有绑定,并且需要重新创建显示每个项目的模板。为了避免重新创建整个列表的性能开销,只需修改现有 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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
你的意思是复选框被选中吗?我认为当您选中/设置复选框时需要更改动画。
它不会出现在 Windows XP 上(这就是为什么我认为这是一个动画),我还没有测试过 Vista :)
祝你好运。
| 归档时间: |
|
| 查看次数: |
2460 次 |
| 最近记录: |