简体   繁体   中英

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

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM