8

I have to following ProgressIndicator

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

and in the ViewModel ascociated with this View I implement

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

The problem is this binding is failing and I don't know why. Using Snoop I have the following

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

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

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

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

I appreciate that there is a binding error, but I am setting the main window's DataContext in the 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 failing?

Thanks for your time.

MoonKnight
  • 23,214
  • 40
  • 145
  • 277

1 Answers1

7

The problem is a bug in the MahApps.ProgressIndicator control.

If you look at the source code, you'll notice that it overwrites the DataContext to itself:

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

As such, you'll need to work around this (stupid) limitation by binding to an element name directly, effectively avoiding using the normal data binding.

For example, if you name your Window (in the xaml), ie:

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

You could do:

<MahAppsControls:ProgressIndicator Width="100"
                               Height="10"
                               VerticalAlignment="Center"
                               ProgressColour="White" 
                               Visibility="{Binding ElementName=Self, Path=DataContext.ProgressVisibility}"/>
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Damn your cool. Did not think that this could be a MahApp bug. Stupid my me. Thanks very much for your time Reed, it is much appreciated... – MoonKnight Aug 14 '13 at 19:49
  • How did you know to look at the source? Do you use the MahApps library? – MoonKnight Aug 14 '13 at 19:50
  • @Killercam No - just a guess, since your xaml/code looked right, but it's using an open source lib, which isn't always top quality ;) – Reed Copsey Aug 14 '13 at 19:55
  • Well, I thank you very much. I am learning MVVM/WPF on my own time with a new application and I am struggling every bit of the way [to be totally honest]! – MoonKnight Aug 14 '13 at 19:59
  • @Killercam I've gotten good feedback from http://reedcopsey.com/series/windows-forms-to-mvvm/ (If you're interested) – Reed Copsey Aug 14 '13 at 20:00
  • Oh great. Massive thanks - people like yourself are what make this site great. Have a good day bud... – MoonKnight Aug 14 '13 at 20:01