开发者

Shortest way to get display value from wpf combobox

开发者 https://www.devze.com 2023-02-19 15:22 出处:网络
To get the currently displayed value from a WPF combobox, I am getting GetSelectedItem (which gives me a dataRowView since my itemSource is a DataView) and then get开发者_运维问答ting the appropriate

To get the currently displayed value from a WPF combobox, I am getting GetSelectedItem (which gives me a dataRowView since my itemSource is a DataView) and then get开发者_运维问答ting the appropriate column.

I was hoping there could be straightforward way to get the Display Value like how we have the SelectedValue property.

Is anyone aware of a better approach?


You use the ADO.Net class DataTable, right?

You can set a displayed value quite straightforward:

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

In this example the combobox displays the value of the column SomeColumn. Put a correct column name instead of this dummy one.

And in code-behind:

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

精彩评论

暂无评论...
验证码 换一张
取 消