wpf 多个 ComboBox.DisplayMemberPath 选项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25346725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple ComboBox.DisplayMemberPath options?
提问by user2453973
I am binding a collection of objects to a ComboBox. What I want to achieve is a combobox that displays two properties of my object. However, I haven't figured out how to make a combobox display multiple DisplayMemberPaths. Is this possible?
我正在将一组对象绑定到ComboBox. 我想要实现的是一个组合框,显示我的对象的两个属性。但是,我还没有弄清楚如何使组合框显示多个DisplayMemberPaths. 这可能吗?
This is how I'm currently setting up my Binding, and setting the DisplayMemberPath:
这就是我目前设置绑定的方式,并设置DisplayMemberPath:
Binding comboBinding = new Binding();
comboBinding.Source = squad_members; //squad_members is the object collection
BindingOperations.SetBinding(Character_ComboBox, ComboBox.ItemsSourceProperty, comboBinding);
Character_ComboBox.DisplayMemberPath = "Name"; //
//Character_ComboBox.DisplayMemberPath = "Name" + "Age"; //failed attempt
Character_ComboBox.SelectedValuePath = "Name";
回答by Rohit Vats
First of all you should do the binding in XAML instead of code behind.
首先,您应该在 XAML 中进行绑定,而不是在后面的代码中进行绑定。
You can provide ItemTemplateand use StringFormatin MultiBindinglike this:
您可以提供ItemTemplate和使用StringFormat中MultiBinding这样的:
<ComboBox ItemsSource="{Binding YourCollection}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} - {1}">
<Binding Path="Name"/>
<Binding Path="Age"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
In case you want to do binding still in code behind.
如果您想在后面的代码中进行绑定。
You can omit setting DisplayMemberPath altogether by overriding ToString()method in your underlying source class. Since, internally ToString() gets called if DisplayMemberPath is not provided.
您可以通过覆盖底层源类中的ToString()方法来完全省略 DisplayMemberPath 的设置。因为,如果未提供 DisplayMemberPath,则会在内部调用 ToString()。
Say you collection is of type List<Person>, so you can override ToString() on Person class:
假设您的 collection 是 type List<Person>,因此您可以在 Person 类上覆盖 ToString() :
public override string ToString()
{
return Name + Age;
}
With this in place, binding will look like this (DisplayMemberPath not needed)
有了这个,绑定看起来像这样(不需要 DisplayMemberPath)
Binding comboBinding = new Binding();
comboBinding.Source = squad_members; //squad_members is the object collection
BindingOperations.SetBinding(Character_ComboBox,
ComboBox.ItemsSourceProperty, comboBinding);
回答by Sajeetharan
Use an ItemTemplate of your ComboBox,Use many TextBlock, as many columns you need. Something like this,
使用 ComboBox 的 ItemTemplate,使用许多 TextBlock,以及您需要的列数。像这样的东西,
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Width="50" Text="{Binding Path=Name}" />
<TextBlock Width="50" Text="{Binding Path=Age}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
回答by Noctis
In addition to the other answer, you can either have a property that returns what you want, or better, a converter that will do the job for you.
除了其他答案之外,您可以拥有一个返回您想要的内容的属性,或者更好的转换器可以为您完成这项工作。
Prop:
支柱:
public int First {get; set;}
public int Second {get; set;}
public int BindToThis {get{return First+Second;}}
Converter: See example In this MSDN page.
转换器:请参阅此 MSDN 页面中的示例。
Mainly along the lines:
主要是沿线:
<TextBlock Name="textBox2" DataContext="{StaticResource NameListData}">
<TextBlock.Text>
<MultiBinding Converter="{StaticResource myNameConverter}"
ConverterParameter="FormatLastFirst">
<Binding Path="FirstName"/>
<Binding Path="LastName"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
and:
和:
public class NameConverter : IMultiValueConverter
{
public object Convert( object[] values
, Type targetType
, object parameter
, CultureInfo culture )
{
string name;
switch ((string)parameter)
{
case "FormatLastFirst":
name = values[1] + ", " + values[0];
break;
case "FormatNormal":
default:
name = values[0] + " " + values[1];
break;
}
return name;
}
// public object[] ConvertBack...
}
回答by Robin
Or you simply override the ToString method in your class e.g.
或者您只需覆盖类中的 ToString 方法,例如
public override string ToString()
{
return Name + " " + Age;
}

