WPF

IsSynchronizedWithCurrentItem

2019-10-11  本文已影响0人  落地成佛

一、概述

如果两个控件都绑定到同一个源(ObservableCollection)集合视图时,该对象会自动绑定到该视图的 CurrentItem。请注意,CollectionViewSource 对象会自动同步货币与所选内容。如果列表控件没有像示例中那样绑定到 CollectionViewSource 对象,则您需要将其 IsSynchronizedWithCurrentItem 属性设置为 true 以达到此目的。

在列表ListBox控件中绑定一个ObservableCollection后,点击列表项时,会自动设置CurrentItem,同时其他控件如果也绑定了该ObservableCollection的话,便会根据CurrentItem重新绑定数据。

二、使用

2.1

  public class People : ObservableCollection<Person>
    {
        public People()
        {
            Add(new Person("Michael", "Alexander", "Bellevue"));
            Add(new Person("Jeff", "Hay", "Redmond"));
            Add(new Person("Christina", "Lee", "Kirkland"));
            Add(new Person("Samantha", "Smith", "Seattle"));
        }
    }

2.2

当ListBox 选择某项后自动通知绑定相同源的ContentControl显示明细

 <ListBox Width="200" IsSynchronizedWithCurrentItem="True"
             ItemsSource="{Binding Source={StaticResource MyFriends}}"/>
 <ContentControl Content="{Binding Source={StaticResource MyFriends}}"
                    ContentTemplate="{StaticResource DetailTemplate}"/>
上一篇下一篇

猜你喜欢

热点阅读