wpf 无法在资源字典中绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16979502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can not bind within resource dictionary
提问by Dave
I'm running a simple MVVM project and fallen at the first hurdle. I'm binding my commands using Josh Smiths Relay Command approach.
我正在运行一个简单的 MVVM 项目,但在第一个障碍时就失败了。我正在使用 Josh Smiths Relay Command 方法绑定我的命令。
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.
问题是,当按钮在 ResourceDictionary 中时,按钮没有绑定。如果我将代码(完全按原样)移动到我的 MainWindow.xaml 中,则代码将按需要执行。
This is my MainWindow.xaml
这是我的 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 the 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 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...
我只能假设在 ResourceDictionary 中绑定时找不到 ViewModel(虽然我不知道为什么我这么认为)。我认为它试图绑定一个额外的级别......
Any way, can some one explain why it's not executing from within the Resource Dictionary please.
无论如何,有人可以解释为什么它不在资源字典中执行。
回答by Viv
This issue seems to have not much to do with the ResourceDictionary than having the parent DataContextpass to the DataTemplate
这个问题似乎与 ResourceDictionary 没有太大关系,而不是让父级DataContext传递给DataTemplate
If you copy the Styleand put it into the Grid.Resourcesand comment the resource dictionary the same behavior can be seen. Also turning on Binding errors should show
如果您复制Style并将其放入Grid.Resources资源字典并对其进行注释,则可以看到相同的行为。同时打开绑定错误应该显示
System.Windows.Data Error: 40 : BindingExpression path error: 'AddCommand' property not found on 'object' ''String'
Fix is pretty much get the DataContextthrough.
修复几乎可以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>
same issue applies to ContentTemplatebut Templateworks fine(It uses a ControlTemplate)
同样的问题适用于ContentTemplate但Template工作正常(它使用ControlTemplate)
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type HeaderedContentControl}">
<StackPanel>
<Button Command="{Binding AddCommand}"
Content="Add" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>

