绑定字典到 SL3 中的组合框
我正在尝试执行以下操作。 我有一个类:
public class TextField {
public string TextType { get; set; }
}
在我的视图中我创建了一个列表:
public TextFieldEditControl()
{
InitializeComponent();
Dictionary<string, string> lst = new Dictionary<string, string>();
lst.Add("SingleLine", "Single line");
lst.Add("MultiLine", "Multi-line");
lst.Add("RichText", "Rich text");
cmbTextType.ItemsSource = lst;
}
在我的 XAML 中我有:
<ComboBox x:Name="cmbTextType" DisplayMemberPath="Value" SelectionChanged="cmbTextType_SelectionChanged"
SelectedItem="{Binding Path=TextType, Mode=TwoWay}" />
问题是,当我检查 TextType 属性的值时,它返回一个像这样的字符串:“[SingleLine,Single line]”而不是只是钥匙。在哪里可以将其设置为仅返回键/值对的键?
I'm trying to do the following.
I have a class:
public class TextField {
public string TextType { get; set; }
}
in my View I created a list:
public TextFieldEditControl()
{
InitializeComponent();
Dictionary<string, string> lst = new Dictionary<string, string>();
lst.Add("SingleLine", "Single line");
lst.Add("MultiLine", "Multi-line");
lst.Add("RichText", "Rich text");
cmbTextType.ItemsSource = lst;
}
in my XAML i have:
<ComboBox x:Name="cmbTextType" DisplayMemberPath="Value" SelectionChanged="cmbTextType_SelectionChanged"
SelectedItem="{Binding Path=TextType, Mode=TwoWay}" />
The problem is that when I check the value of the TextType property, it returns a string like that: "[SingleLine, Single line]" instead of just the Key. Where can I set it to return only the key for a Key/Value Pair?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
绑定到
SelectedValue
属性而不是SelectedItem
并指定SelectedValuePath="Key"
。Bind to the
SelectedValue
property instead ofSelectedItem
and specifySelectedValuePath="Key"
.在您的标题中,您指定了 Silverlight 3,遗憾的是它没有 Anthony 提到的 SelectedValue 和 SelectedValuePath 属性。这意味着您需要采取一种令人讨厌的解决方法才能使其正常工作。我在我的这篇 Silverlight 2 时代文章中的标题为“ComboBox 噩梦”的部分中对此进行了讨论:http://www.silverlightshow.net/items/Building-a-Silverlight-Line-Of-Business-Application-Part-5.aspx。这是 Silverlight 2 中的一个难题,直到 Silverlight 4 才得到修复。
希望这会有所帮助...
Chris
In your title you specified Silverlight 3, which unfortunately didn't have the SelectedValue and SelectedValuePath properties that Anthony mentions. This means that you need to do a nasty workaround to get it to work. I discuss it here in this Silverlight 2 era article of mine, in a section titled "The ComboBox Nightmare": http://www.silverlightshow.net/items/Building-a-Silverlight-Line-Of-Business-Application-Part-5.aspx. It was a pain in Silverlight 2, and wasn't fixed until Silverlight 4.
Hope this helps...
Chris