Search code examples
c#xamlmvvmwin-universal-app

StackPanel visibility not updated when dependency property changed


I'm currently developping an universal app in C#/XAML with MVVM (not MVVM Light) and I have trouble for the XAML part.

I'd like to display one or another StackPanel when a dependency property changed in my ViewModel. I think the code speaks for itself.

<StackPanel Visibility="{Binding MyProperty, Converter={StaticResource BooleanToVisibilityConverter}}">
    <!-- Some content -->
</StackPanel>

<StackPanel Visibility="{Binding MyProperty, Converter={StaticResource InvertBooleanToVisibilityConverter}}">
    <!-- Some another content -->
</StackPanel>

And here is the definition of the dependency property.

public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register(
    "MyProperty",
    typeof (bool),
    typeof (MyViewModel),
    new PropertyMetadata(true));

public bool MyProperty
{
    get { return (bool) GetValue(MyPropertyProperty); }
    set { SetValue(MyPropertyProperty, value); OnPropertyChanged(); // Implemented by ReSharper }
}

I guess you figure it out that MyProperty is a boolean that I convert into a Visibility via the converters. So, when MyProperty changed in the ViewModel, the view isn't updated.

I already tried to use the UpdateSourceTrigger property but it's not working. Also, I have no binding error and converters are working fine (I only see one StackPanel at the app launch).

Please keep in mind that I don't want to use the code behind part unless there is no other solution.

Thanks for your help.


Solution

  • I finaly gave up and used the code behind part and it's working fine now.