将 Combobox 与 Dictionary 对象绑定,但在后面的代码中设置所选值不生效
这是在 XAML 中实例化的 ComboBox
<Combobox ItemsSource="{Binding Path=Delimiters}" DisplayMemberPath="Key"
SelectedValue="{Binding Path=SelectedDelimiter, UpdateSourceTrigger=PropertyChanged}" />
下面是视图模型中相应的绑定,其中在构造函数中填充了 Dictionary
:
private IDictionary<string,string> _delimiters;
public IDictionary<string,string> Delimiters
{
get{return _delimiters;}
set{_delimiters = value; RaisePropertyChanged("Delimiters");}
}
private KeyValuePair <string, string> _selectedDelimiter;
public KeyValuePair <string, string> SelectedDelimiter
{
get{return _selectedDelimiter;}
set{
if(value.Key != _selectedDelimiter.Key || value.Value != _selectedDelimiter.Value)
{
var prevDelimiter = _selectedDelimiter;
_selectedDelimiter = value;
if(IllegalDelimiter.Contains(_selectedDelimiter)
{
MessageBox.Show("errror", "error");
_selectedDelmiter = prevDelimiter;
}
RaisePropertyChanged("SelectedDelimiter");
}
}
}
我在将所选值绑定回来时遇到问题。 Dictionary
正在绑定,当我对 UI ComboBox
进行更改时,该设置被正确触发。在 if 语句中检查其是否为非法分隔符,它确实将所选值设置回其后面代码中的原始值,但它不会填充到 ComboBox
UI (我看到 get从 UI 触发的访问器)。就像设置 SelectedValue
并没有真正对 UI 做任何事情。
如果有人能指出我正确的方向吗?
Here is my ComboBox
instantiated in the XAML
<Combobox ItemsSource="{Binding Path=Delimiters}" DisplayMemberPath="Key"
SelectedValue="{Binding Path=SelectedDelimiter, UpdateSourceTrigger=PropertyChanged}" />
Here is the corresponding bindings in the view model with the Dictionary
populated in the constructor:
private IDictionary<string,string> _delimiters;
public IDictionary<string,string> Delimiters
{
get{return _delimiters;}
set{_delimiters = value; RaisePropertyChanged("Delimiters");}
}
private KeyValuePair <string, string> _selectedDelimiter;
public KeyValuePair <string, string> SelectedDelimiter
{
get{return _selectedDelimiter;}
set{
if(value.Key != _selectedDelimiter.Key || value.Value != _selectedDelimiter.Value)
{
var prevDelimiter = _selectedDelimiter;
_selectedDelimiter = value;
if(IllegalDelimiter.Contains(_selectedDelimiter)
{
MessageBox.Show("errror", "error");
_selectedDelmiter = prevDelimiter;
}
RaisePropertyChanged("SelectedDelimiter");
}
}
}
I am having trouble with binding the selected value back. The Dictionary
is getting bound and when I make a changed to the UI ComboBox
, the setting is being fired correctly. In the if statement to check if its an illegal delimiter, it does set the selected value back to its original value in the code behind, but it doesn't populate to the ComboBox
UI (I see the get accessor firing from the UI). Its like setting SelectedValue
doesn't really do anything to the UI.
if someone could point me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果要使用
SelectedValue
,则应该设置SelectedValuePath
,大概是Value
。SelectedValue
正在进行“选择”,因此您的属性类型也应该不同。如果您想保留整个
KeyValuePair
,您应该绑定SelectedItem
。You should set
SelectedValuePath
if you are going to use theSelectedValue
, presumably toValue
.SelectedValue
is doing a "selection", so your property type should be different as well.If you want to retain the whole
KeyValuePair
you should bind theSelectedItem
instead.花了一段时间才弄清楚,我正确地设置了一些东西,但是因为我在设置器中更新相同的属性,所以我需要使用调度程序创建一个新线程来正确地进行更新。
Took a while to figure out, I was setting things correctly, but because I was updating the same property in the setter, I needed to use a dispatcher to create a new thread to do the update correctly.