首页 > 其他分享 >【WPF】Tabcontrol的IsSynchronizedWithCurrentItem属性

【WPF】Tabcontrol的IsSynchronizedWithCurrentItem属性

时间:2022-10-15 08:44:08浏览次数:82  
标签:控件 ObservableCollection string persons 绑定 public IsSynchronizedWithCurrentItem W

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

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

下面举个例子说明下吧:

XMAL代码:

    <StackPanel>
        <ListBox Margin="5" Name="firstListBox" DisplayMemberPath="FirstName" IsSynchronizedWithCurrentItem="True"></ListBox>
        <ListBox Margin="5" Name="secondListBox" DisplayMemberPath="FirstName" IsSynchronizedWithCurrentItem="True"></ListBox>
    </StackPanel>

 

c#后台代码:

    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public Person(string f,string l)
        {
            FirstName = f;
            LastName = l;
        }
    }
 
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public static ObservableCollection<Person> persons { get; set; } = new ObservableCollection<Person>();
        public MainWindow()
        {
            persons.Add(new Person("MeiLin","Xu"));
            persons.Add(new Person("JieShi", "Jiang"));
            InitializeComponent();
            firstListBox.ItemsSource = persons;
            secondListBox.ItemsSource = persons;
        }
    }

我们可以看到下面的效果:

 

 

 

 

标签:控件,ObservableCollection,string,persons,绑定,public,IsSynchronizedWithCurrentItem,W
From: https://www.cnblogs.com/cdaniu/p/16793548.html

相关文章

  • WPF基于Prsim框架的学习
    Prism框架代码:前后端分离,依赖注入IOC,不是基础应用实战数据库:服务器数据SqlSever、本地缓存Sqlite数据库的添加:去程序包控制台先使用字符串add—migration—表名然后upd......
  • WPF结合阿里巴巴矢量图标库使用ttf格式的图标字体
    一、阿里巴巴图标矢量库(https://www.iconfont.cn/)1、创建字体工程 2、往工程里添加图标 3、生成字体文件 4、window下安装字体库并查看查看对应的unicode码 5......
  • WPF 入门教程DispatcherTimer计时器
    在WinForms中,有一个名为Timer的控件,它可以在给定的时间间隔内重复执行一个操作。WPF也有这种可能性,但我们有DispatcherTimer控件,而不是不可见的控件。它几乎做同样的......
  • WPF 模仿V_S_Co_d_e
    WPF简单模仿V_S_C_o_de开源具体功能也不多,难度也不大,自定义了若干控件实现以上程序:CusWindow:自定义窗体,WindowChrome方案,用于添加HeaderContent在标题中显示内容......
  • 如何在WPF中使用矢量图标
    WPF中除了可以用传统的图片格式(jpg,png)做中控件的图标外,更常见的时使用Geometry作为图标。本文将讲解如何从网上找到svg数据,然后嵌入到WPF里。准备数据阿里矢量图【提......
  • WPF listbox中添加index
    关键代码:如果中在ItemsControl中加入Index,"RelativeSource={RelativeSourceAncestorType=ListBoxItem}"可以写成,"RelativeSource={RelativeSourceTemplatedParent}"但是......
  • WPF ListBox添加新数据时自动滚到最后一行
    Xaml文件代码如下:<ListBoxx:Name="lstBox"Height="200"AlternationCount="100000"ItemsSource="{BindingLogs}"><List......
  • 【WPF】应用程序 本地化
    本地化就是根据不同地区语言显示不同的文字。本文环境:vs2022+.net6.0新的本地化方式使用资源字典,然后动态引用资源字典,以下以UI界面汉化为例:新建一个文件夹 Languag......
  • WPF之界面出现锯齿形的噪点
    项目中出现重启电脑后(软件自动启动)点击软件界面时偶尔出现锯齿噪点现象,最后发现是图片硬解码绘制的BUG。使用软解就可以避开。处理方法:在窗口的Loaded事件加这句varhw......
  • An ItemsControl is inconsistent with its items source - WPF Listbox
    AnItemsControlisinconsistentwithitsitemssource-WPFListbox 原来代码这么写的privateICollection<string>filterList=newList<string>();    ......