ResourceDictionary 中 DataTemplate 的代码隐藏
我正在尝试使用代码隐藏来支持 DataTemplate 中的事件处理程序。当下面的代码是 Window 的代码隐藏时,它可以正常工作,但不能用于 ResourceDictionary。当将代码放入 ResourceDictionary 的代码隐藏时,该代码甚至不会编译。
我知道命令在这里是更好的选择,但这主要是一个测试,以确保我可以在需要时处理 ResourceDictionary 中资源的事件。我的目标是更好地组织我的代码,但这并不是我认为单独的 ResourceDictionary 文件将提供的简单的“包含”行为。
在 MainWindow.xaml 中:
<Window x:Class="Wizbang.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:DevComponents.WpfEditors;assembly=DevComponents.WpfEditors"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
xmlns:local ="clr-namespace:Wizbang"
xmlns:m ="clr-namespace:Wizbang.Model"
xmlns:vm="clr-namespace:Wizbang.ViewModel"
xmlns:vw="clr-namespace:Wizbang.View"
DataContext="{Binding Path=Main, Source={StaticResource Locator}}"
Title="Wizbang" Height="760" Width="1335" WindowStartupLocation="CenterScreen">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary Source="Resources/MainWindowResources.xaml" />
</ResourceDictionary>
</Window.Resources>
在 MainWindow.xaml.cs 和 MainWindowResources.xaml.cs 的代码隐藏中,相同的代码:
private void btnSave_Click(object sender, RoutedEventArgs e)
{
//switch item template
Button btn = (Button)sender;
//command contains the list item
ContentControl itm = (ContentControl)btn.CommandParameter;
itm.SetValue(ContentTemplateProperty, this.FindResource("DetailedTemplate") as DataTemplate);
//this.UpdateLayout();
}
当我将 ResourceDictionary 内联在 MainWindow.xaml 中,并将代码隐藏在 MainWindow.xaml.cs 中时,所有内容作品。当我尝试对 ResourceDictionary 使用单独的文件时,代码无法编译。编译器抱怨最后一行:
itm.SetValue(ContentTemplateProperty, this.FindResource("DetailedTemplate") as DataTemplate);
this.FindResource() 不是有效方法,并且找不到“ContentTemplateProperty”:
错误4名称 “ContentTemplateProperty”不 存在于当前 上下文 C:...\Visual Studio 2010\Projects\Wizbang\Wizbang\Resources\MainWindowResources.xaml.cs 36 26 Wizbang
错误 5“Wizbang.Resources.MainWindowResources” 不包含以下定义 “FindResource”并且没有扩展方法 “FindResource”接受第一个 类型参数 'Wizbang.Resources.MainWindowResources' 可以找到(您是否缺少 using 指令或程序集 参考?)C:...\Visual Studio 2010\Projects\Wizbang\Wizbang\Resources\MainWindowResources.xaml.cs 36 56 Wizbang
如果我删除最后一行,代码将编译并运行,但该按钮没有任何功能。我认为我的问题是从 ResourceDictionary 的角度映射最后一行的引用,但我不确定为什么它应该不同。
感谢您的任何想法。
账单
I am attempting to use code-behind to support an event handler in a DataTemplate. The below code works fine when it is the code-behind for a Window, but not for a ResourceDictionary. The code will not even compile when put in the code-behind for the ResourceDictionary.
I know that Commands is the better option here, but this is largely a test to make sure I can handle events on resources in a ResourceDictionary, if needed. My goal is to better organize my code, but this is not the straightforward "include" behavior that I thought a separate ResourceDictionary file would provide.
In MainWindow.xaml:
<Window x:Class="Wizbang.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:DevComponents.WpfEditors;assembly=DevComponents.WpfEditors"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
xmlns:local ="clr-namespace:Wizbang"
xmlns:m ="clr-namespace:Wizbang.Model"
xmlns:vm="clr-namespace:Wizbang.ViewModel"
xmlns:vw="clr-namespace:Wizbang.View"
DataContext="{Binding Path=Main, Source={StaticResource Locator}}"
Title="Wizbang" Height="760" Width="1335" WindowStartupLocation="CenterScreen">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary Source="Resources/MainWindowResources.xaml" />
</ResourceDictionary>
</Window.Resources>
In code-behind MainWindow.xaml.cs and MainWindowResources.xaml.cs, the same code:
private void btnSave_Click(object sender, RoutedEventArgs e)
{
//switch item template
Button btn = (Button)sender;
//command contains the list item
ContentControl itm = (ContentControl)btn.CommandParameter;
itm.SetValue(ContentTemplateProperty, this.FindResource("DetailedTemplate") as DataTemplate);
//this.UpdateLayout();
}
When I keep the ResourceDictionary inline in MainWindow.xaml, and put the code-behind in MainWindow.xaml.cs, everything works. When I attempt to use a separate file for ResourceDictionary, the code does not compile. The compiler complains about the last line:
itm.SetValue(ContentTemplateProperty, this.FindResource("DetailedTemplate") as DataTemplate);
The this.FindResource() is not a valid method, and "ContentTemplateProperty" is not found:
Error 4 The name
'ContentTemplateProperty' does not
exist in the current
context C:...\Visual Studio
2010\Projects\Wizbang\Wizbang\Resources\MainWindowResources.xaml.cs 36 26 WizbangError 5 'Wizbang.Resources.MainWindowResources'
does not contain a definition for
'FindResource' and no extension method
'FindResource' accepting a first
argument of type
'Wizbang.Resources.MainWindowResources'
could be found (are you missing a
using directive or an assembly
reference?) C:...\Visual Studio
2010\Projects\Wizbang\Wizbang\Resources\MainWindowResources.xaml.cs 36 56 Wizbang
If I remove that last line, the code compiles and runs, but the button has no functionality. I think my issue is mapping that last line's references from the perspective of a ResourceDictionary, but I am not sure why it should be different.
Thanks for any thoughts.
Bill
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为代码不再位于窗口类中。您必须再次找到它(或者您想要在其上放置模板的任何控件)。
This is because the code no longer is in the window class. You have to find it again (or whatever control you want to place the template on).
我认为使用命令将是一种更简洁的方法。
I think that using Commands would be a much cleaner approach.