How to bind combobox display string and value string through list wpf c#

时间:2017-06-15 09:49:04

标签: c# wpf combobox

Need to bind a combobox through list in wpf binding its display string and value string . I am only able to bind the display text , how to to bind value text in pair with display text ?

List<string> value = new List<string>();
value.Add("a");
value.Add("b");
route_select_points.ItemsSource = value;

The value text stays hidden in frontend

Here is the example of sample data (updated detailed)

https://i.stack.imgur.com/uEQmQ.png

1 个答案:

答案 0 :(得分:0)

It looks like you need to use ListView with GridView, see next:

<ListView Name="route_select_points" Margin="5">
          <ListView.View>
                    <GridView>
                        <GridView.Columns>
                            <GridViewColumn Header="Value" DisplayMemberBinding="{Binding Path=ValueText}"></GridViewColumn>
                            <GridViewColumn Header="Display" DisplayMemberBinding="{Binding Path=DisplayText}"></GridViewColumn>
                        </GridView.Columns>
                    </GridView>
          </ListView.View>
     </ListView>

And for binding you need to define your class with properties: "ValueText", "DisplayText", as example I suggest

public class SomeData
{
    public string ValueText { get; set; }
    public string DisplayText { get; set; }
}

And the creating of list will be:

var value = new List<SomeData>();
value.Add(new SomeData(){ ValueText = "1", DisplayText = "a"});
value.Add(new SomeData(){ ValueText = "2", DisplayText = "b"});
route_select_points.ItemsSource = value;