Binding Error Using the Machapers ProgressIndicator

I have to follow the ProgressIndicator

 <MahAppsControls:ProgressIndicator Width="100" Height="10" VerticalAlignment="Center" ProgressColour="White" Visibility="{Binding ProgressVisibility}"/> 

and in the ViewModel connected to this view I implements

 private Visibility progressVisibility = Visibility.Collapsed; public Visibility ProgressVisibility { get { return progressVisibility; } set { if (value == progressVisibility) return; progressVisibility = value; this.OnPropertyChanged("ProgressVisibility"); } } 

The problem is that this binding fails, and I don't know why. With Snoop, I have

System.Windows.Data error: 40: BindingExpression path error: the 'ProgressVisibility' property was not found on the 'object' '' ProgressIndicator '(Name =' progressIndicator ')'. BindingExpression: Path = ProgressVisibility; DataItem = 'ProgressIndicator' (Name = 'progressIndicator');

target element is 'ProgressIndicator' (Name = 'progressIndicator'); target property - "Visibility" (type "Visibility") System.Windows.Data error: 40: BindingExpression path error: the 'ProgressVisibility' property was not found on the 'object' '' ProgressIndicator '(Name =' progressIndicator ')'. BindingExpression: Path = ProgressVisibility; DataItem = 'ProgressIndicator' (Name = 'progressIndicator');

target element is 'ProgressIndicator' (Name = 'progressIndicator'); target property - "Visibility" (type "Visibility") System.Windows.Data error: 40: BindingExpression path error: the 'ProgressVisibility' property was not found on the 'object' '' ProgressIndicator '(Name =' progressIndicator ')'. BindingExpression: Path = ProgressVisibility; DataItem = 'ProgressIndicator' (Name = 'progressIndicator');

target element is 'ProgressIndicator' (Name = 'progressIndicator'); target is "Visibility" (type "Visibility")

I appreciate that there is a binding error, but I set the main DataContext window in App.xaml.cs via

 MainWindow window = new MainWindow(); MainWindowViewModel mainWindowViewModel = new MainWindowViewModel(); // When the ViewModel asks to be closed, close the window. EventHandler handler = null; handler = delegate { mainWindowViewModel.RequestClose -= handler; window.Close(); }; mainWindowViewModel.RequestClose += handler; // Allow all controls in the window to bind to the ViewModel by setting the // DataContext, which propagates down the element tree. window.DataContext = mainWindowViewModel; window.Show(); 

So, why is the binding error?

Thank you for your time.

+8
c # wpf binding mahapps.metro
source share
1 answer

The problem is an error in the MahApps.ProgressIndicator control.

If you look at the source code , you will notice that it overwrites the DataContext for itself:

 public ProgressIndicator() { InitializeComponent(); this.DataContext = this; 

Thus, you need to get around this (stupid) restriction by binding to the element name directly, effectively avoiding using normal data binding.

For example, if you name your Window (in xaml), that is:

 <Window ... Name="Self"> <!--... 

You can do:

 <MahAppsControls:ProgressIndicator Width="100" Height="10" VerticalAlignment="Center" ProgressColour="White" Visibility="{Binding ElementName=Self, Path=DataContext.ProgressVisibility}"/> 
+7
source share

All Articles