【问题标题】:How to refer to class attribute when setting ItemsSource设置 ItemsSource 时如何引用类属性
【发布时间】:2020-06-13 08:02:12
【问题描述】:

我想将列表中的TabItems绑定到TabControl

<TabControl Name="TabsControl" ItemsSource="{Binding}"/>

TabObject 在后面的代码中生成:

private List<TabObject> tabsList = new List<TabObject>();    
TabObject MyTab = new TabObject(Tabitem tabitem, object genericObject);
tabsList.Add(MyTab);
TabsControl.DataContext = tabList

如何仅将 TabItems 对象从 tabsList 绑定到 TabsControl,是否可以不为 TabItems 创建单独的列表?

【问题讨论】:

    标签: c# list class object binding


    【解决方案1】:

    您可以使用DisplayMemberPathSelectedValuePath 将tabItem 用作名称和selectedValue。

    您的TabControl 最终会是这样的:

    <TabControl Name="TabsControl" ItemsSource="{Binding}" DisplayMemberPath="Tabitem" SelectedValuePath="Tabitem"/>
    

    我写了Tabitem,因为我不知道TabObject 类型的对象的Tabitem 类型的属性名称被调用。如果您以 tabObject.Item 的形式访问该项目,显然您只需要在那里写 Item

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多