views:

30

answers:

1

Hello all,

I am able to bind a "MyDictionary.Value" to a ComboBox and see the Values in it:

Dictionary<string, Friends> friend_list = new Dictionary<string, Friends>();
Friend_chat_list.ItemsSource = friend_list.Values;

And here is the XAML code:

<ComboBox x:Name="Friend_chat_list" Width="90" ItemsSource="{Binding}" SelectionChanged="Friend_chat_list_SelectionChanged">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}" TextTrimming="WordEllipsis"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

Here is the class I used to create my dictionnay:

public class Friends
{
    public string Friend_guid { get; set; }
    public string Name { get; set; }
    public string Message { get; set; }
    public string FriendAvatar { get; set; }
    public string Status { get; set; }
    public string Status_Image { get; set; }
    public List<Chat> chat_list { get; set; }
}

What I would like to do is bind the string "Friend_guid" as a ValueMember and not a DisplayMEmber, that I could retrieve on event "SelectionChanged". The user would only see the "Friend_name" in the combobox but it would return me "Friend_guid" instead of "Friend_name".

Help would be greatly appreciated.

Ephismen.

+1  A: 

xaml ---

< ComboBox x:Name="Friend_chat_list" Width="90" ItemsSource="{Binding MyFirendsList.Values}" SelectionChanged="Friend_chat_list_SelectionChanged" DisplayMemberPath="Name" SelectedValuePath="Friend_guid" >

    </ComboBox>

Code Behind

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        MyFirendsList  = new Dictionary<string, Friends>();

        MyFirendsList.Add("1", new Friends() { Friend_guid = Guid.NewGuid().ToString(),Name = "Saurabh" });
        MyFirendsList.Add("2", new Friends() { Friend_guid = Guid.NewGuid().ToString(), Name = "Nitya" });

        this.DataContext = this;

    }

    public Dictionary<string, Friends> MyFirendsList { get; set; }

    private void Friend_chat_list_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
          MessageBox.Show((sender as ComboBox).SelectedValue.ToString());
    }
}

public class Friends
{
    public string Friend_guid { get; set; }
    public string Name { get; set; }
    public string Message { get; set; }
    public string FriendAvatar { get; set; }
    public string Status { get; set; }
    public string Status_Image { get; set; }

} 
saurabh
That binding works but how do you retrieve the value of SelectedValuePath in C#, when i try to do it I only get the type of it: Friend_guid. I would like to get the real value behind it.
Ephismen
R u geeting Name correctly displayed?
saurabh
you need to just use Combobox.SelectedValue.
saurabh
Thank You Saurabh it worked perfectly, I guess I misunderstood the difference between all the terms used. Anyway thanks again!
Ephismen