0

In my program I have a mainWindow that contains a contentPresenter. The mainWindow has a ViewModel called MainWindowViewModel. This viewModel contains properties that are bound to items in the mainWindow.

The Content of my contentPresenter is represented by a UserControl->ViewModel->Model structure.

This is the xaml of the contentPresenter that I am working with.

MainWindow.xaml:

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

LeftWidget and LeftWidgetEnabled are both properties located in MainWindowViewModel. However, the BindingExpression path error that I receive has to do with LeftWidgetEnabled. For some reason my program is looking for the property in the ViewModel of the contentPresenter's UserControl. This doesn't make much sense to me, because the program deals with the LeftWidget property correctly.

Both properties are located in MainWindowViewModel, so why would the program be looking elsewhere for the LeftWidgetEnabled property? How can I fix this?

Also Note: The way that I set the DataContext of my UserControls are like so...

Under <Window.Resources... in mainWindow.xaml:

<DataTemplate DataType="{x:Type project:LeftWidgetViewModel}">
    <local:LeftWidgetUserControl/>
</DataTemplate>
Eric after dark
  • 1,768
  • 4
  • 31
  • 79

2 Answers2

1

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?

J King
  • 4,108
  • 10
  • 53
  • 103
  • Thank you so much, this did it! Now what do I need to check for? You said that something else might be going on. – Eric after dark Nov 06 '13 at 18:15
  • glad to help! "Something else" only applied if this didn't work. Datacontexts and binding are great but can be frustrating as you have to be explicit in what datacontext your objects are using when you have nested objects (and in xaml everything is nested). SO what I mean is use the output window during debug to track down binding errors (these will not throw general exceptions but can really slow down your app). When you find them I use a program called snoop to look into the wpf app. It lets you examine the visual tree and object bindings for trouble shooting these issues. – J King Nov 06 '13 at 18:19
  • if this answer is right then the information in the question was wrong :) – blindmeis Nov 07 '13 at 11:49
0

if LeftWidget and LeftWidgetEnabled are in the same datacontext and if LeftWidget is working. then i would say you should check your property in your viemodel wether its really LeftWidgetEnabled.

the xaml looks good for me.

EDIT: LeftWidgetEnabled should be typeof bool not bool?

blindmeis
  • 22,175
  • 7
  • 55
  • 74