1. <tfoot id='jZLdy'></tfoot>

    <small id='jZLdy'></small><noframes id='jZLdy'>

  2. <legend id='jZLdy'><style id='jZLdy'><dir id='jZLdy'><q id='jZLdy'></q></dir></style></legend>

      <bdo id='jZLdy'></bdo><ul id='jZLdy'></ul>

    1. <i id='jZLdy'><tr id='jZLdy'><dt id='jZLdy'><q id='jZLdy'><span id='jZLdy'><b id='jZLdy'><form id='jZLdy'><ins id='jZLdy'></ins><ul id='jZLdy'></ul><sub id='jZLdy'></sub></form><legend id='jZLdy'></legend><bdo id='jZLdy'><pre id='jZLdy'><center id='jZLdy'></center></pre></bdo></b><th id='jZLdy'></th></span></q></dt></tr></i><div id='jZLdy'><tfoot id='jZLdy'></tfoot><dl id='jZLdy'><fieldset id='jZLdy'></fieldset></dl></div>
    2. WPF:绑定到 ListBoxItem.IsSelected 不适用于屏幕外项目

      WPF: Binding to ListBoxItem.IsSelected doesn#39;t work for off-screen items(WPF:绑定到 ListBoxItem.IsSelected 不适用于屏幕外项目)

      <small id='ztc0i'></small><noframes id='ztc0i'>

        <bdo id='ztc0i'></bdo><ul id='ztc0i'></ul>
        <tfoot id='ztc0i'></tfoot>
        1. <i id='ztc0i'><tr id='ztc0i'><dt id='ztc0i'><q id='ztc0i'><span id='ztc0i'><b id='ztc0i'><form id='ztc0i'><ins id='ztc0i'></ins><ul id='ztc0i'></ul><sub id='ztc0i'></sub></form><legend id='ztc0i'></legend><bdo id='ztc0i'><pre id='ztc0i'><center id='ztc0i'></center></pre></bdo></b><th id='ztc0i'></th></span></q></dt></tr></i><div id='ztc0i'><tfoot id='ztc0i'></tfoot><dl id='ztc0i'><fieldset id='ztc0i'></fieldset></dl></div>

          1. <legend id='ztc0i'><style id='ztc0i'><dir id='ztc0i'><q id='ztc0i'></q></dir></style></legend>

                  <tbody id='ztc0i'></tbody>
              • 本文介绍了WPF:绑定到 ListBoxItem.IsSelected 不适用于屏幕外项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在我的程序中,我有一组视图模型对象来表示 ListBox 中的项目(允许多选).viewmodel 有一个 IsSelected 属性,我想将其绑定到 ListBox,以便在 viewmodel 中而不是在列表框本身中管理选择状态.

                In my program I have a set of view-model objects to represent items in a ListBox (multi-select is allowed). The viewmodel has an IsSelected property that I would like to bind to the ListBox so that selection state is managed in the viewmodel rather than in the listbox itself.

                但是,显然 ListBox 不维护大多数屏幕外项目的绑定,因此通常 IsSelected 属性未正确同步.这是一些演示问题的代码.第一个 XAML:

                However, apparently the ListBox doesn't maintain bindings for most of the off-screen items, so in general the IsSelected property is not synchronized correctly. Here is some code that demonstrates the problem. First XAML:

                <StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock>Number of selected items: </TextBlock>
                        <TextBlock Text="{Binding NumItemsSelected}"/>
                    </StackPanel>
                    <ListBox ItemsSource="{Binding Items}" Height="200" SelectionMode="Extended">
                        <ListBox.ItemContainerStyle>
                            <Style TargetType="{x:Type ListBoxItem}">
                                <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
                            </Style>
                        </ListBox.ItemContainerStyle>
                    </ListBox>
                    <Button Name="TestSelectAll" Click="TestSelectAll_Click">Select all</Button>
                </StackPanel>
                

                C# 全选处理程序:

                private void TestSelectAll_Click(object sender, RoutedEventArgs e)
                {
                    foreach (var item in _dataContext.Items)
                        item.IsSelected = true;
                }
                

                C# 视图模型:

                public class TestItem : NPCHelper
                {
                    TestDataContext _c;
                    string _text;
                    public TestItem(TestDataContext c, string text) { _c = c; _text = text; }
                
                    public override string ToString() { return _text; }
                
                    bool _isSelected;
                    public bool IsSelected
                    {
                        get { return _isSelected; }
                        set {
                            _isSelected = value; 
                            FirePropertyChanged("IsSelected");
                            _c.FirePropertyChanged("NumItemsSelected");
                        }
                    }
                }
                public class TestDataContext : NPCHelper
                {
                    public TestDataContext()
                    {
                        for (int i = 0; i < 200; i++)
                            _items.Add(new TestItem(this, i.ToString()));
                    }
                    ObservableCollection<TestItem> _items = new ObservableCollection<TestItem>();
                    public ObservableCollection<TestItem> Items { get { return _items; } }
                
                    public int NumItemsSelected { get { return _items.Where(it => it.IsSelected).Count(); } }
                }
                public class NPCHelper : INotifyPropertyChanged
                {
                    public event PropertyChangedEventHandler PropertyChanged;
                    public void FirePropertyChanged(string prop)
                    {
                        if (PropertyChanged != null)
                            PropertyChanged(this, new PropertyChangedEventArgs(prop));
                    }
                }
                

                可以观察到两个不同的问题.

                Two separate problems can be observed.

                1. 如果单击第一个项目,然后按 Shift+End,则应选择所有 200 个项目;但是,标题报告只选择了 21 个项目.
                2. 如果您单击全选",则确实选择了所有项目.如果您然后单击 ListBox 中的一个项目,您会希望取消选择其他 199 个项目,但这不会发生.相反,只有屏幕上的项目(以及其他一些项目)被取消选择.除非您首先从头到尾滚动列表,否则所有 199 个项目都不会被取消选择(即使这样,奇怪的是,如果您使用小滚动框执行滚动,它也不起作用).

                我的问题是:

                1. 有人能准确解释为什么会这样吗?
                2. 我可以避免或解决这个问题吗?

                推荐答案

                ListBox 默认情况下是 UI 虚拟化的.这意味着在任何给定时刻,只有 ItemsSource 中的可见项目(以及几乎可见"项目的一小部分)将实际呈现.这就解释了为什么更新 source 可以按预期工作(因为这些项目始终存在),但只是导航 UI 却不能(因为这些项目的视觉表示是动态创建和销毁的,并且永远不会同时存在.)

                ListBox is, by default, UI virtualized. That means that at any given moment, only the visible items (along with a small subset of "almost visible" items) in the ItemsSource will actually be rendered. That explains why updating the source works as expected (since those items always exist,) but just navigating the UI doesn't (since the visual representations of those items are created and destroyed on the fly, and never exist together at once.)

                如果您想关闭此行为,一种选择是在您的 ListBox 上设置 ScrollViewer.CanContentScroll=False.这将启用平滑"滚动,并隐式关闭虚拟化.要显式禁用虚拟化,您可以设置 VirtualizingStackPanel.IsVirtualizing=False.

                If you want to turn off this behaviour, one option is to set ScrollViewer.CanContentScroll=False on your ListBox. This will enable "smooth" scrolling, and implicitly turn off virtualization. To disable virtualization explicitly, you can set VirtualizingStackPanel.IsVirtualizing=False.

                这篇关于WPF:绑定到 ListBoxItem.IsSelected 不适用于屏幕外项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)
                WPF ListBox not updating with the ItemsSource(WPF ListBox 未使用 ItemsSource 更新)
                Problem getting list box items added through jquery in code behind(在后面的代码中通过 jquery 添加列表框项目时出现问题)
                Items in ObservableCollectionlt;stringgt; do not update when bound to a WPF ListBox(ObservableCollectionlt;stringgt; 中的项目绑定到 WPF ListBox 时不更新)
                Selected item in list box is null(列表框中的选定项为空)

                    • <small id='D8nig'></small><noframes id='D8nig'>

                        <bdo id='D8nig'></bdo><ul id='D8nig'></ul>
                      • <i id='D8nig'><tr id='D8nig'><dt id='D8nig'><q id='D8nig'><span id='D8nig'><b id='D8nig'><form id='D8nig'><ins id='D8nig'></ins><ul id='D8nig'></ul><sub id='D8nig'></sub></form><legend id='D8nig'></legend><bdo id='D8nig'><pre id='D8nig'><center id='D8nig'></center></pre></bdo></b><th id='D8nig'></th></span></q></dt></tr></i><div id='D8nig'><tfoot id='D8nig'></tfoot><dl id='D8nig'><fieldset id='D8nig'></fieldset></dl></div>
                        <legend id='D8nig'><style id='D8nig'><dir id='D8nig'><q id='D8nig'></q></dir></style></legend>
                          <tbody id='D8nig'></tbody>
                        <tfoot id='D8nig'></tfoot>