Code behind DataTemplate in ResourceDictionary

I am trying to use code to support an event handler in a DataTemplate. The code below works fine when it is code for a window, but not for ResourceDictionary. Code will not compile even when entering code for ResourceDictionary.

I know that it is better to use Commands here, but this is largely a test so that I can handle events on resources in a ResourceDictionary, if necessary. My goal is to better organize my code, but this is not a simple "inclusion" of the behavior that I considered a separate ResourceDictionary file.

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 the source code, MainWindow.xaml.cs and MainWindowResources.xaml.cs have 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 save the ResourceDictionary inline in MainWindow.xaml and put the code in MainWindow.xaml.cs, everything works. When I try 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); 

This .FindResource () is not a valid method, and "ContentTemplateProperty" was 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 Wizbang

Error 5 'Wizbang.Resources.MainWindowResources' does not contain a definition for "FindResource" and the extension method "FindResource" takes the first argument of the type 'Wizbang.Resources.MainWindowResources' (you don’t see the link using the directive or assembly?) C: ... \ Visual Studio 2010 \ Projects \ Wizbang \ Wizbang \ Resources \ MainWindowResources.xaml.cs 36 56 Wizbang

If I delete this last line, the code compiles and runs, but the button has no functionality. I think my problem is matching last-line links in terms of a ResourceDictionary, but I'm not sure why this should be otherwise.

Thanks for any thoughts.

Bill

+4
source share
2 answers

This is because the code is no longer in the window class. You should find it again (or whatever element you want to place in the template).

 Window parentWindow = Window.GetWindow(btn); itm.SetValue(Window.ContentTemplateProperty, parentWindow.FindResource("DetailedTemplate") as DataTemplate); 
+3
source

I think using Commands would be a cleaner approach.

+3
source

Source: https://habr.com/ru/post/1347663/


All Articles