Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在词典中绑定和检索信息<;Tkey,TValue>;到组合框?_C#_Data Binding_Dictionary_Silverlight 4.0_Combobox - Fatal编程技术网

C# 如何在词典中绑定和检索信息<;Tkey,TValue>;到组合框?

C# 如何在词典中绑定和检索信息<;Tkey,TValue>;到组合框?,c#,data-binding,dictionary,silverlight-4.0,combobox,C#,Data Binding,Dictionary,Silverlight 4.0,Combobox,我可以将“MyDictionary.Value”绑定到组合框并查看其中的值: Dictionary<string, Friends> friend_list = new Dictionary<string, Friends>(); Friend_chat_list.ItemsSource = friend_list.Values; Dictionary-friend_-list=new Dictionary(); Friend_chat_list.ItemsSource

我可以将“MyDictionary.Value”绑定到组合框并查看其中的值:

Dictionary<string, Friends> friend_list = new Dictionary<string, Friends>();
Friend_chat_list.ItemsSource = friend_list.Values;
Dictionary-friend_-list=new Dictionary();
Friend_chat_list.ItemsSource=Friend_list.Values;
下面是XAML代码:

<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>

这是我用来创建我的词汇的类否:

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; }
}
公共课好友
{
公共字符串Friend_guid{get;set;}
公共字符串名称{get;set;}
公共字符串消息{get;set;}
公共字符串FriendAvatar{get;set;}
公共字符串状态{get;set;}
公共字符串状态_图像{get;set;}
公共列表聊天列表{get;set;}
}
我想做的是将字符串“Friend_guid”绑定为ValueMember而不是DisplayMEmber,我可以在事件“SelectionChanged”中检索该字符串。用户只会在组合框中看到“Friend\u name”,但它会返回“Friend\u guid”而不是“Friend\u name”

非常感谢您的帮助

Ephismen.

xaml---

< 组合框x:Name=“Friend\u chat\u list”Width=“90”ItemsSource=“{Binding MyFirendsList.Values}” SelectionChanged=“朋友\聊天\列表\选择已更改” DisplayMemberPath=“名称” SelectedValuePath=“朋友\u guid” >


代码隐藏

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; }

} 
公共部分类窗口1:窗口
{
公共窗口1()
{
初始化组件();
MyFirendsList=新字典();
添加(“1”,新朋友(){Friend\u guid=guid.NewGuid().ToString(),Name=“Saurabh”});
添加(“2”,新朋友(){Friend\u guid=guid.NewGuid().ToString(),Name=“Nitya”});
this.DataContext=this;
}
公共字典MyFirendsList{get;set;}
私人无效好友\u聊天\u列表\u选择已更改(对象发件人,选择已更改的联系人)
{
Show((发件人作为组合框)。SelectedValue.ToString());
}
}
公课朋友
{
公共字符串Friend_guid{get;set;}
公共字符串名称{get;set;}
公共字符串消息{get;set;}
公共字符串FriendAvatar{get;set;}
公共字符串状态{get;set;}
公共字符串状态_图像{get;set;}
} 

绑定可以工作,但是如何在C#中检索SelectedValuePath的值,当我尝试这样做时,我只得到它的类型:Friend#u guid。我想得到它背后的真正价值。你只需要使用Combobox.SelectedValue。谢谢你Saurab它工作得很好,我想我误解了所有术语之间的区别。无论如何,再次感谢!
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; }

}