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.
Dave
source share