WPF 双向数据绑定 - 数据视图的可编辑组合框
<ComboBox Height="23" Margin="52,64,33,0" Name="comboBox1"
IsSynchronizedWithCurrentItem="True"
IsEditable="True"
DisplayMemberPath="Value"
SelectedItem="{Binding Path=Number, Mode=TwoWay}"
/>
public class Number : INotifyPropertyChanged
{
private string value;
public string Value
{
get
{
return value;
}
set
{
this.value = value;
this.PropertyChanged(this, new PropertyChangedEventArgs("Value"));
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged = delegate { };
#endregion
}
comboBox1.ItemsSource = new Number[] { new Number() { Value = "One" },
new Number() { Value = "Two" },
new Number() { Value = "Three" }};
当我编辑组合框文本时,我的绑定数据集没有修改。 即,目标到源的绑定没有发生。
<ComboBox Height="23" Margin="52,64,33,0" Name="comboBox1"
IsSynchronizedWithCurrentItem="True"
IsEditable="True"
DisplayMemberPath="Value"
SelectedItem="{Binding Path=Number, Mode=TwoWay}"
/>
public class Number : INotifyPropertyChanged
{
private string value;
public string Value
{
get
{
return value;
}
set
{
this.value = value;
this.PropertyChanged(this, new PropertyChangedEventArgs("Value"));
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged = delegate { };
#endregion
}
comboBox1.ItemsSource = new Number[] { new Number() { Value = "One" },
new Number() { Value = "Two" },
new Number() { Value = "Three" }};
My binded data set is not modifying when i am editing combobox text.
ie., target to source binding is not happening.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加 Josh 建议的内容......
首先,您应该考虑使用 diff 变量名称,然后使用“值”,
其次,如果值没有更改,则不应触发“PropertyChanged”事件。
将其添加到属性设置器...
第三,您不绑定到数据实例,而是绑定到类类型
第四,您应该将组合框中的 ItemSource 设置为 ObservableCollection
ObservableCollection
Number >
最后,您应该查看 Bea 关于调试数据绑定的精彩博客文章 她有很多很好的例子。
好的,现在我可以访问我的编译器了...这就是您需要做的。
首先,您要绑定的“Number”属性位于哪里?您无法绑定回作为组合框源的列表。
您需要将 ElementName 添加到绑定,或将 DataContext 设置为包含 Number 属性的对象。其次,该 Number 属性,无论它在哪里,都必须是 Notify 或 DependencyProperty。
例如,您的 Window 类将如下所示......
而您的 window.xaml 将如下所示......
adding to what Josh advises....
first, you should think about using a diff variable name then "value",
second, you shouldnt fire the "PropertyChanged" event if the value is not changing.
add this to the property setter....
third, your not binding to an instance of your data, your binding to your class type
fourth, you should set the ItemSource in your combobox to an
ObservableCollection< Number >
lastly, you should check out Bea's great blog entry about debugging databinding. She has many great examples.
ok, so now that i have access to my compiler.... here is what you need to do.
Firstly, WHERE is the "Number" property located that you are binding to? you can not bind back to the list that is the source of your combobox.
you need to add an ElementName to the binding, or set the DataContext to the object that contains the Number property. Second, that Number property, wherever it might be, needs to be either a Notify or a DependencyProperty.
for example, your Window class would look like this.....
and your window.xaml would look like this...