1

I have created a dependency property on my WPF usercontrol which would be databound in my parent usercontrol. I have implemented the INotifyPropertyChanged on my viewmodel to send notifications when the value changes.

User Control code:

public bool IsVisibile {
get { return (bool) GetValue(IsVisibileProperty); }
set { SetValue(IsVisibileProperty, value); }}

public static readonly DependencyProperty IsVisibileProperty =
        DependencyProperty.Register("IsVisibile", typeof(bool), typeof(UserControl),
                                    new PropertyMetadata(default(bool), VisiblePropertyChangedCallback));

    private static void VisiblePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
    {
        if (dependencyPropertyChangedEventArgs.NewValue != null)
        {
            ((UserControl) dependencyObject).IsVisibile = (bool) dependencyPropertyChangedEventArgs.NewValue;
        }

    }

Parent User Control usage:

<uc:UserControl IsVisible="{Binding IsViewModelVisible, UpdateSourceTrigger=PropertyChanged}"

If the "IsViewModelVisible" changes then the property changed event handler is not called and the property is not refreshed.

Any thoughts?

4

2 回答 2

1

In the PropertyChangedCallback, you are getting notified when your IsVisibile property has changed. Now you set the same property another time, that's pointless.

And even worse, setting the property effectively removes the binding. Hence you won't get notified about any subsequent changes of the binding source property.

Do not set the property again in the callback. Just do whatever shall happen when the property changes. I guess you might want to set the control's Visibility.

于 2013-03-06T18:45:08.760 回答
0

I think the code is right, the only thing that may cause the DP not raising maybe could be that you are setting typeof(UserControl) instead you should to use typeof(YourControlType). Test this and feedback me. Hope can helps...


EDIT

Yes, Clemens is right, the property that you must set is the Visibility property, also you may use a value converter, BoolToVisibilityConverter for instace.

于 2013-03-06T18:47:59.357 回答