简体   繁体   English

ContentPresenter发生奇怪的BindingExpression路径错误

[英]Strange BindingExpression path error with ContentPresenter

In my program I have a mainWindow that contains a contentPresenter . 在我的程序中,我有一个mainWindow,其中包含一个contentPresenter The mainWindow has a ViewModel called MainWindowViewModel . mainWindow有一个称为MainWindowViewModel的ViewModel。 This viewModel contains properties that are bound to items in the mainWindow. 此viewModel包含绑定到mainWindow中的项目的属性。

The Content of my contentPresenter is represented by a UserControl->ViewModel->Model structure. 我的contentPresenterContent由UserControl-> ViewModel-> Model结构表示。

This is the xaml of the contentPresenter that I am working with. 这是我正在使用的contentPresenter的xaml。

MainWindow.xaml: MainWindow.xaml:

<ContentPresenter Content="{Binding LeftWidget}" IsEnabled="{Binding LeftWidgetEnabled}" ... />

LeftWidget and LeftWidgetEnabled are both properties located in MainWindowViewModel . LeftWidgetLeftWidgetEnabled都是位于MainWindowViewModel属性。 However, the BindingExpression path error that I receive has to do with LeftWidgetEnabled . 但是,我收到的BindingExpression path errorLeftWidgetEnabled For some reason my program is looking for the property in the ViewModel of the contentPresenter 's UserControl. 由于某种原因,我的程序正在contentPresenter的UserControl的ViewModel中寻找属性。 This doesn't make much sense to me, because the program deals with the LeftWidget property correctly. 这对我来说没有多大意义,因为该程序正确处理了LeftWidget属性。

Both properties are located in MainWindowViewModel , so why would the program be looking elsewhere for the LeftWidgetEnabled property? 这两个属性都位于MainWindowViewModel ,那么为什么程序要在其他地方寻找LeftWidgetEnabled属性? How can I fix this? 我怎样才能解决这个问题?

Also Note: The way that I set the DataContext of my UserControls are like so... 另请注意:设置UserControls的DataContext的方式类似于...

Under <Window.Resources... in mainWindow.xaml: 在mainWindow.xaml中的<Window.Resources...下:

<DataTemplate DataType="{x:Type project:LeftWidgetViewModel}">
    <local:LeftWidgetUserControl/>
</DataTemplate>

chnage the binding path to (this assumes main window is in fact a window object): 更改绑定路径(假设主窗口实际上是窗口对象):

IsEnabled={Binding DataContext.LeftWidgetEnabled, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}

does that help? 有帮助吗? If so then you need to examine the datacontext of your objects as there might be something else going on 如果是这样,那么您需要检查对象的数据上下文,因为可能还会发生其他情况

also, does the datatype of LeftWidgetEnabled match what is expected by the IsEnabled Property, ie boolean to boolean? 另外,LeftWidgetEnabled的数据类型是否与IsEnabled属性所期望的匹配,即boolean到boolean?

if LeftWidget and LeftWidgetEnabled are in the same datacontext and if LeftWidget is working. 如果LeftWidget和LeftWidgetEnabled在相同的数据上下文中,并且LeftWidget在工作。 then i would say you should check your property in your viemodel wether its really LeftWidgetEnabled . 那么我想说的是,如果它确实是LeftWidgetEnabled,则应该在viemodel中检查您的属性。

the xaml looks good for me. xaml对我来说很好。

EDIT: LeftWidgetEnabled should be typeof bool not bool? 编辑:LeftWidgetEnabled应该是typeof布尔不是布尔?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM