WPF MVVM - the user control will not be attached to visibility if I set the DataContext in the code

I wanted to make quick user control for my application, but to save things in MVVM style, I thought I installed the DataContext XAML in the code located in my UserControl.

i.e.

DataContext="{Binding RelativeSource={RelativeSource Self}}"

This allows me to bind XAML to properties in my code.

Everything went well until I came to bind the visibility of the control instance to the visibility property in the ViewModel.

<Controls:RoundProgress Visibility="{Binding ProgressVisibility}" Width="100" Height="100"></Controls:RoundProgress>

Visibility no longer works - if I delete my voyages using the DataContext from User Control - visibility works!

Can someone fix me right? Thanks

+3
source share
2

DataContext UserControl XAML. , DataContext Binding ProgressVisibility UC ViewModel. DataContext UC:

<UserControl x:Class...>
  <Grid DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}">
    ...
  </Grid>
</UserControl>
+10

DataContext . DataContext , , .

, viewmodel, :

<Controls:RoundProgress Visibility="{Binding ViewModel.ProgressVisibility}" ...

datacontext ( ) , , - :

<Button Text="{Binding ButtonText, Source={RelativeSource Mode=FindAncestor, AncestorType={x:Type MyUserControl}}}"/>
+1

Source: https://habr.com/ru/post/1762861/


All Articles