WPF: Binding to MainWindow Property

隐身守侯 提交于 2021-02-05 08:46:26

问题


I am having an issue with binding to a parent MainWindow property, MainContentVisibility. I have the following code:

MainWindow.xaml

<Window x:Class="CallTracker.WPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:v="clr-namespace:MyProgram.WPF.Views"
        DataContext="{Binding MainPageViewModel, Source={StaticResource Locator}}">

        <StackPanel>
            <v:CompanyInfoUserControl Width="800" Visibility="{Binding MainContentVisibility, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" />
            <v:MainContentUserControl Width="800" Visibility="{Binding Path=MainContentVisibility, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" />
        </StackPanel>   
</Window>

Currently my two user controls are nothing more than a stack panel with a text block until I figure out this Visibility issue.

I can use Snoop to check my MainPageViewModel and I can see that the property MainContentVisibilty is set to "Collapsed" but the CompanyInfoUserControl shows an issue with its Visibility binding:

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

Can someone explain what I am doing wrong here?

EDIT

I tried paul's suggestion and now I get this as the binding error from Snoops:

System.Windows.Data Error: 40 : BindingExpression path error: 'MainContentVisibility' property not found on 'object' ''MainPageViewModel' (HashCode=63642613)'. BindingExpression:Path=MainContentVisibility; DataItem='MainPageViewModel' (HashCode=63642613); target element is 'CompanyInfoUserControl' (Name=''); target property is 'Visibility' (type 'Visibility')

SOLUTION

I wasn't actually binding to my data model but rather the XAML object (in this case MainWindow.xaml) In order to bind to the data I had to add the reference 'DataContext' as follows:

<v:CompanyInfoUserControl Visibility="{Binding DataContext.MainContentVisibility, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />

回答1:


I take it that MainContentVisibility is not on the ViewModel?

If you want to access it on the main window's property away from the viewmodel, then provide a name for the window such as x:Name="MyMainWindow" and the access it in the binding

Visibility="{Binding MainContentVisibility, ElementName=MyMainWindow}"



回答2:


You already have the DataContext of the Window set to the MainPageViewModel. Child elements will inherit this DataContext so there is no need for your relative pathing. All you need is:

<v:CompanyInfoUserControl Width="800" Visibility="{Binding Path=MainContentVisibility}" />

Your current code is looking for a property called MainContentVisibility on the Window control itself, not the view model.



来源:https://stackoverflow.com/questions/27111681/wpf-binding-to-mainwindow-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!