奇怪的 BindingMode=TwoWay 行为
有这样的代码
public int SelectedPage
{
get { return (int)GetValue(SelectedPageeProperty); }
set
{
SetValue(SelectedPageeProperty, value);
if (NavigationCommands.Refresh.CanExecute(null, this))
NavigationCommands.Refresh.Execute(null, this);
}
}
// Using a DependencyProperty as the backing store for SelectedPage. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectedPageeProperty =
DependencyProperty.Register("SelectedPagee", typeof(int), typeof(DataBaseSettings), new UIPropertyMetadata(0));
wpf :
<ComboBox SelectedItem="{Binding Path=SelectedPage, Mode=TwoWay}" />
奇怪的是,如果静态依赖属性的命名类似于公共属性 SelectedPage,则绑定不起作用。但如果我稍微将 dp 重命名为 SelectedPagee,并带有 2 ee,它就可以工作。有人有可以解释这种现象的想法吗?
have this code
public int SelectedPage
{
get { return (int)GetValue(SelectedPageeProperty); }
set
{
SetValue(SelectedPageeProperty, value);
if (NavigationCommands.Refresh.CanExecute(null, this))
NavigationCommands.Refresh.Execute(null, this);
}
}
// Using a DependencyProperty as the backing store for SelectedPage. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectedPageeProperty =
DependencyProperty.Register("SelectedPagee", typeof(int), typeof(DataBaseSettings), new UIPropertyMetadata(0));
wpf :
<ComboBox SelectedItem="{Binding Path=SelectedPage, Mode=TwoWay}" />
The strange thing about this is if the static dependency property is named like the public property SelectedPage, the binding does not work. But if i slighty rename the dp to SelectedPagee with 2 ee's it works. Does anybody have an idea that could explain this phenomenon?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您像这样重命名属性,则绑定将找不到它并使用 CLR 属性的 setter,否则将被完全忽略(这就是为什么您不应该在其中包含任何代码的原因)就像你所做的那样),并且你的包装器中有一些刷新逻辑(如前所述,不应该存在),它可能会刷新与绑定有关的东西,使你认为它“有效”。
如果您需要对更改的属性执行其他逻辑,请向字段的元数据注册添加
DependencyPropertyChanged
回调。If you rename the property like this the binding will not find it and use the setter of the CLR-property which otherwise will be completely ignored (that is the reason why you are not supposed to have any code in it like you do), and there is some refresh logic in your wrapper (which as noted should not be there) which probably does refresh something that has to do with the binding making you think that it "works".
If you need additional logic performed on property changed add a
DependencyPropertyChanged
callback to the meta-data registration of the field.