从wpf组合框获取显示值的最短方法

时间:2011-03-25 19:00:10

标签: wpf combobox

要从WPF组合框获取当前显示的值,我收到GetSelectedItem(由于我的dataRowViewitemSource,因此我获得DataView)然后获取适当的栏目。

我希望可以直接获得显示值,就像我们拥有SelectedValue属性一样。

有人知道更好的方法吗?

1 个答案:

答案 0 :(得分:2)

您使用ADO.Net类DataTable,对吗?

您可以非常直接地设置显示值:

<ComboBox x:Name="myComboBox" ItemsSource="{Binding}" DisplayMemberPath="SomeColumn"
          SelectedValuePath="SomeColumn"/>

在此示例中,组合框显示列SomeColumn的值。输入正确的列名而不是这个虚拟名称。

在代码隐藏中:

myComboBox.DataContext = myDataSet.Customers; //any table
var selectedValue = myComboBox.SelectedValue;    //The displayed value (SomeColumn)
var fullRow = myComboBox.SelectedITem;        //dataRowView, I think