2
votes

Really annoying issue. The code seems to work fine, but when run, I see this error being thrown up:

System.Windows.Data Error: 40 : BindingExpression path error: 'IsOpen' property not found on 'object' ''MainViewModel' (HashCode=33664731)'. BindingExpression:Path=IsOpen; DataItem='MainViewModel' (HashCode=33664731); target element is 'Popup' (Name='LoginPopup'); target property is 'IsOpen' (type 'Boolean')

I understand there is some sort of issue where IsOpen property of LoginPopup binding is being attempted on the MainViewModel and failing because there is no IsOpen property in the MainViewModel. Fine and good, but the DataContext I'm binding to is not the mainviewmodel, but the LoginPopupViewModel (LoginPopup in the ViewModelLocator class). Intellisense seems to agree with me that things are bound to the LoginPopupViewModel, as if I type other things in the Binding portion of IsOpen, it complains that, "Cannot resolve property in data context of LoginPopupViewModel", as it should.

Here's a portion of the XAML code:

<Popup x:Name="LoginPopup"  PlacementTarget="{Binding ElementName=LoginButton}"  
       Placement="Bottom"
       HorizontalOffset="-40" VerticalOffset="35" StaysOpen="False" 
       IsOpen=" {Binding IsOpen}"
       DataContext="{Binding LoginPopup, Mode=OneWay, Source={StaticResource Locator}}" 
       Grid.ColumnSpan="3" Grid.Column="0"
           Margin="0,0,0.333,0"
           Grid.Row="0" Grid.RowSpan="2">

I'm not exactly a master of WPF, so I'm probably missing something, or the sytax is wrong here. Any help would be fantastic! Thanks!

1

1 Answers

1
votes

You are getting that error because DataContext binding is afterIsOpen binding, so when the parser comees across IsOpen the DataContext is currently MainViewModel (set by dependency property inheritance). Then WPF processes the DataContext={..} and re-evaluated the IsOpen binding.

E.g. 2 bindings ultimately the same, but one showing an error

This gives an error in output, but works

    <TextBlock Text="{Binding TextB}" DataContext="{Binding B}"></TextBlock>

This doesn't give error

    <TextBlock DataContext="{Binding B}" Text="{Binding TextB}"></TextBlock>

Original Answer


The error you are getting is because this:

IsOpen="{Binding IsOpen}"

cannot be bound successfully.

You are binding the DataContext of it to {StaticResource Locator}, and a property LoginPopup on that resource. I am assuming Locator.LoginPopup return a MainViewModel?

What is Locator.LoginPopup and what are you trying to achieve by IsOpen="{Binding IsOpen}"? Is that a property on some view model?