Unable to link in resource dictionary

I am starting a simple MVVM project and have fallen on the first hurdle. I am linking my teams using the Josh Smiths Relay Command approach.

The problem is that the button is optional when the button is in a ResourceDictionary. If I translate the code (exactly as it is) into my MainWindow.xaml, then the code will execute as desired.

This is my MainWindow.xaml

<Window x:Class="ForJon.Views.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vm="clr-namespace:ForJon.Ui.ViewModels" Title="MainWindow" Height="350" Width="525"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="160" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Grid.Resources> <ResourceDictionary Source="Resources\ResourceDictionary.xaml" /> </Grid.Resources> <Grid.DataContext> <vm:MainWindowViewModel /> </Grid.DataContext> <HeaderedContentControl Header="Options" Style="{StaticResource LeftMenu}" Grid.Column="0" / > </Grid> </Window> 

And a resource dictionary

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:view="clr-namespace:ForJon.Ui.Views" xmlns:viewModel="clr-namespace:ForJon.Ui.ViewModels" > <Style x:Key="LeftMenu" TargetType="HeaderedContentControl"> <Setter Property="HeaderTemplate"> <Setter.Value> <DataTemplate> <StackPanel> <Button Content="Add" Command="{Binding AddCommand}" /> </StackPanel> </DataTemplate> </Setter.Value> </Setter> <Setter Property="Width" Value="160"/> </Style> </ResourceDictionary> 

I can only assume that when binding in a ResourceDictionary that it cannot find the ViewModel (although I don't know why I think so). I think he is trying to tie an extra level down ...

In any case, can someone explain why it is not executed from the resource dictionary.

+7
source share
1 answer

This problem does not seem to have anything to do with ResourceDictionary, except that the parent DataContext goes to the DataTemplate

If you copy Style and put it in Grid.Resources and comment on the resource dictionary, you can see the same behavior. Also the inclusion of binding errors should show

 System.Windows.Data Error: 40 : BindingExpression path error: 'AddCommand' property not found on 'object' ''String' 

The fix is ​​largely related to the DataContext .

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Style x:Key="LeftMenu" TargetType="HeaderedContentControl"> <Setter Property="HeaderTemplate"> <Setter.Value> <DataTemplate> <StackPanel DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type HeaderedContentControl}}, Path=DataContext}"> <Button Command="{Binding AddCommand}" Content="Add" /> </StackPanel> </DataTemplate> </Setter.Value> </Setter> <Setter Property="Width" Value="160" /> </Style> </ResourceDictionary> 

the same problem applies to ContentTemplate , but Template works fine (it uses a ControlTemplate )

 <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type HeaderedContentControl}"> <StackPanel> <Button Command="{Binding AddCommand}" Content="Add" /> </StackPanel> </ControlTemplate> </Setter.Value> </Setter> 
+8
source

All Articles