[英]How to bind class member in a collection
I want to bind a class member of an element I added to a collection to DisplayMemberPath
. 我想将添加到集合中的元素的类成员绑定到
DisplayMemberPath
。 I bound a ObservableCollection
to ComboBox.ItemSource
and want to show the property name
in the combobox's list which is a member of my class AxisBase
. 我将
ObservableCollection
绑定到ComboBox.ItemSource
并希望在组合框列表中显示属性name
,该列表是我的类AxisBase
的成员。
Here is my code: 这是我的代码:
private ObservableCollection<AxisBase> axis { get; set; }
axis
I use to hold elements of the following class 我用来容纳以下类的元素的
axis
class AxisBase
{
...
public string name { get; set; }
...
}
This is how my xaml looks like 这就是我的xaml的样子
<ComboBox Name="comboBox_AchsenListe" DisplayMemberPath="{Binding ElementName=axis, Path=AxisBase.name}" ItemsSource="{Binding ElementName=_MainWindow, Path=axis}"</ComboBox>
Does anyone know how to bind name
to DisplayMemberPath
? 有人知道如何将
name
绑定到DisplayMemberPath
吗?
change DisplayMemberPath value 更改DisplayMemberPath值
DisplayMemberPath="name"
SelectedValuePath="name"
and look at this question 看看这个问题
I have created sample application for you here the xaml 我在这里为您创建了示例应用程序xaml
<Window x:Class="ComboBoxSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ComboBox
ItemsSource="{Binding Path=AxisBases}"
DisplayMemberPath="Name"
SelectedValuePath="Name"
Height="23" HorizontalAlignment="Left" Margin="200,134,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" />
</Grid>
here is code behind 这是背后的代码
using System.Collections.ObjectModel;
using System.Windows;
namespace ComboBoxSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
AxisBases = new ObservableCollection<AxisBase>
{
new AxisBase {Name = "Firts"},
new AxisBase {Name = "Second"},
new AxisBase {Name = "Third"}
};
//Set the data context for use binding
DataContext = this;
}
public ObservableCollection<AxisBase> AxisBases { get; set; }
}
public class AxisBase
{
public string Name { get; set; }
}
} }
It works OK and binding also in combo box appears 3 items. 它可以正常工作,并且在组合框中的绑定也出现3个项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.