WPF:绑定到 MainWindow 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27111681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:43:24  来源:igfitidea点击:

WPF: Binding to MainWindow Property

c#wpfxamldata-bindingvisibility

提问by BrianKE

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

我在绑定到父 MainWindow 属性 MainContentVisibility 时遇到问题。我有以下代码:

MainWindow.xaml

主窗口.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:

我可以使用 Snoop 检查我的 MainPageViewModel,我可以看到属性 MainContentVisibilty 设置为“Collapsed”,但 CompanyInfoUserControl 显示其可见性绑定存在问题:

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')

System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“MainWindow”(名称 =“”)上找不到“MainContentVisibility”属性。BindingExpression:Path=MainContentVisibility; DataItem='MainWindow' (Name=''); 目标元素是 'CompanyInfoUserControl' (Name=''); 目标属性是“可见性”(类型“可见性”)

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:

我尝试了 paul 的建议,现在我将其作为 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')

System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“MainPageViewModel”(HashCode=63642613)上找不到“MainContentVisibility”属性。BindingExpression:Path=MainContentVisibility; DataItem='MainPageViewModel' (HashCode=63642613); 目标元素是 'CompanyInfoUserControl' (Name=''); 目标属性是“可见性”(类型“可见性”)

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:

我实际上并没有绑定到我的数据模型,而是绑定到 XAML 对象(在本例中为 MainWindow.xaml)为了绑定到数据,我必须添加引用“DataContext”,如下所示:

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

回答by ΩmegaMan

I take it that MainContentVisibilityis not on the ViewModel?

我认为它MainContentVisibility不在 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

如果您想在远离视图模型的主窗口的属性上访问它,请为窗口提供一个名称,例如x:Name="MyMainWindow"并在绑定中访问它

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

回答by Paul Abbott

You already have the DataContextof the Windowset to the MainPageViewModel. Child elements will inherit this DataContextso there is no need for your relative pathing. All you need is:

您已经将DataContextWindow设置为MainPageViewModel。子元素将继承它,DataContext因此不需要您的相对路径。所有你需要的是:

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

Your current code is looking for a property called MainContentVisibilityon the Windowcontrol itself, not the view model.

您当前的代码正在寻找MainContentVisibilityWindow控件本身而不是视图模型上调用的属性。