不能资源字典中的绑定 [英] Can not bind within resource dictionary
问题描述
我运行一个简单的MVVM项目,并在第一障碍下降。我结合使用约什 - 史密斯接力指挥方式我的命令。
I'm running a simple MVVM project and fallen at the first hurdle. I'm binding my commands using Josh Smiths Relay Command approach.
问题是,按钮不具约束力当按钮在ResourceDictionary中。如果我移动代码(完全一样的)到我的MainWindow.xaml然后根据需要在代码执行。
The problem is, the button isn't binding when the button is in the ResourceDictionary. If I move the code (exactly as is) into my MainWindow.xaml then the code executes as desired.
这是我的MainWindow.xaml
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>
和资源字典
<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>
我只能假设,在ResourceDictionary中绑定时,它找不到视图模型(虽然我不知道为什么,我觉得)。我认为这是试图绑定一个额外的水平下降......
I can only assume that when binding in the ResourceDictionary that it can't find the ViewModel (although I don't know why I think that). I think it's trying to bind an extra level down...
什么办法,能有人解释为什么它不从资源字典吧。
Any way, can some one explain why it's not executing from within the Resource Dictionary please.
推荐答案
此问题似乎已经没有太多做的ResourceDictionary中比有父的DataContext
传递给的DataTemplate
This issue seems to have not much to do with the ResourceDictionary than having the parent DataContext
pass to the DataTemplate
如果您复制风格
并把它放到 Grid.Resources
和评论资源字典相同的行为就可以看出。也开启绑定错误应该显示
If you copy the Style
and put it into the Grid.Resources
and comment the resource dictionary the same behavior can be seen. Also turning on Binding errors should show
System.Windows.Data Error: 40 : BindingExpression path error: 'AddCommand' property not found on 'object' ''String'
修复是相当多获得的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>
同样的问题也适用于的ContentTemplate
,但模板
工作正常(它使用了控件模板
)
same issue 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>
这篇关于不能资源字典中的绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!