I have created class that inherits from Window and I am applying control template to it:
public class BaseSearchWindow : Window { static BaseSearchWindow() { DefaultStyleKeyProperty.OverrideMetadata(typeof(BaseSearchWindow), new FrameworkPropertyMetadata(typeof(BaseSearchWindow))); } public BaseSearchWindow() { Uri uri = new Uri("/WPFLibs;component/Resources/StyleResources.xaml", UriKind.Relative); ResourceDictionary Dict = Application.LoadComponent(uri) as ResourceDictionary; this.Style = Dict["WindowTemplate"] as Style; } Than I want to find some control in control template:
public override void OnApplyTemplate() { RibbonCommand searchCommand = this.Template.FindName("searchCommand", this) as RibbonCommand; //doesn't work, searchCommand is null searchCommand.CanExecute += CanExecuteRibbonCommand; } But it is allways null. I tried it in inherited class and it works, but I want it in my base class, so I don't have to search for it every time I use that class. This works:
public partial class MainWindow : BaseSearchWindow { public MainWindow() { InitializeComponent(); RibbonCommand searchCommand = this.Template.FindName("searchCommand", this) as RibbonCommand; searchCommand.CanExecute += CanExecuteRibbonCommand; }