开发者

Strange BindingMode=TwoWay behaviour

开发者 https://www.devze.com 2023-04-01 00:24 出处:网络
have this code public int SelectedPage { get { return (int)GetValue(SelectedPageeProperty); } set { SetValue(SelectedPageeProperty, value);

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 UIProp开发者_如何学GoertyMetadata(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?


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.

... new UIPropertyMetadata(0, SelectedPageChanged);

private static void SelectedPageChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    var @this = (DataBaseSettings)sender;
    if (NavigationCommands.Refresh.CanExecute(null, @this))
        NavigationCommands.Refresh.Execute(null, @this);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号