我试图用一个例子来说明我的问题。
我的组合框有一个对象列表作为itemssource。在我的例子中,它是一个人员列表。在组合框中,我想显示该人的名字和姓氏。但我想在人物对象的“所有者”属性中保存此人的姓氏。
我的猜测是我将SelectedValue绑定到我的属性,将SelectedValuePath绑定到组合框架中属性的名称。
我已经使用Google搜索并尝试查看其他版本,但没有任何效果。
如果我使用SelectedItem而不是SelectedValue使用相同的绑定,至少在该属性中写入“tostring”函数的值。遗憾的是,该解决方案不适合我的其余程序,因为我不想覆盖“ToString”。
Xaml:
<Window x:Class="MultiColumnCombobox.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
x:Name="window">
<Grid>
<ComboBox Height="23"
Margin="72,12,86,0"
Name="ComboBox1"
VerticalAlignment="Top"
SelectedValue="{Binding CurrentHouse.Owner, ElementName=window, Mode=TwoWay}"
SelectedValuePath="LastName"
ItemsSource="{Binding PersonList, ElementName=window, Mode=Default}">
<ComboBox.ItemTemplate>
<DataTemplate>
<WrapPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=FirstName}"
Padding="10,0,0,0" />
<TextBlock Text="{Binding Path=LastName}"
Padding="10,0,0,0" />
</WrapPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Button Height="23"
Click="PrintButton_Click"
HorizontalAlignment="Left"
Margin="12,0,0,9"
Name="PrintButton"
VerticalAlignment="Bottom"
Width="75">Print</Button>
</Grid>
C#
using System.Collections.Generic;
using System.Windows;
using System;
namespace MultiColumnCombobox
{
public partial class Window1 : Window
{
private List<Person> _PersonList = new List<Person>();
public List<Person> PersonList
{
get { return _PersonList; }
set { _PersonList = value; }
}
private House _CurrentHouse = new House { Owner = "Green", Number = "11" };
public House CurrentHouse
{
get { return _CurrentHouse; }
}
public Window1()
{
InitializeComponent();
PersonList.Add(new Person {FirstName = "Peter", LastName = "Smith"});
PersonList.Add(new Person {FirstName = "John", LastName = "Meyer"});
PersonList.Add(new Person {FirstName = "Fritz", LastName = "Green"});
}
private void PrintButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(CurrentHouse.Owner + ":" + CurrentHouse.Number);
}
}
public class House
{
public string Owner { get; set; }
public string Number { get; set; }
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
也许有人有想法,
基督教
答案 0 :(得分:0)
如果我理解你的问题,你想在ComboBox中取当前所选Person的姓氏,并将其值设置为CurrentHouse.Owner属性。如果是这样,请查看以下内容并查看是否有意义。具体来说,请查看ComboBox的SelectedValue
和SelectedValuePath
属性的设置。
代码背后
public partial class MultiColumnCombobox : Window
{
public List<Person> PersonList { get; set; }
public House CurrentHouse { get; set; }
public MultiColumnCombobox()
{
InitializeComponent();
PersonList = new List<Person>
{
new Person
{
FirstName = "Peter",
LastName = "Smith"
},
new Person
{
FirstName = "John",
LastName = "Meyer"
},
new Person
{
FirstName = "Fritz",
LastName = "Green"
}
};
CurrentHouse = new House
{
Owner = "Green",
Number = "11"
};
this.DataContext = this;
ShowCurrentHouseInfo();
}
private void OnComboBoxChanged( object sender, SelectionChangedEventArgs e )
{
ShowCurrentHouseInfo();
}
private void ShowCurrentHouseInfo()
{
MyTextBlock.Text = string.Format(
"CurrentHouse.Owner: {0} : CurrentHouse.Number: {1}",
CurrentHouse.Owner, CurrentHouse.Number);
}
}
public class House
{
public string Owner { get; set; }
public string Number { get; set; }
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
<强> XAML 强>
<StackPanel>
<ComboBox
Margin="10"
Name="ComboBox1"
VerticalAlignment="Top"
SelectedValue="{Binding CurrentHouse.Owner}"
SelectedValuePath="LastName"
SelectionChanged="OnComboBoxChanged"
ItemsSource="{Binding PersonList}">
<ComboBox.ItemTemplate>
<DataTemplate>
<WrapPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=FirstName}"
Padding="10,0,0,0" />
<TextBlock Text="{Binding Path=LastName}"
Padding="10,0,0,0" />
</WrapPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<TextBlock x:Name="MyTextBlock" Margin="10" />
</StackPanel>