我创建类,从窗口继承和我正在申请控制模板将其:寻找控制在WPF控件模板
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;
}
比我想找到控制模板中的一些控制:
public override void OnApplyTemplate() {
RibbonCommand searchCommand = this.Template.FindName("searchCommand", this) as RibbonCommand;
//doesn't work, searchCommand is null
searchCommand.CanExecute += CanExecuteRibbonCommand;
}
但它是无效的。 我在继承类中尝试过它,它可以工作,但我希望它在我的基类中,所以我不必每次使用该类时都要搜索它。 这工作:
public partial class MainWindow : BaseSearchWindow {
public MainWindow() {
InitializeComponent();
RibbonCommand searchCommand = this.Template.FindName("searchCommand", this) as RibbonCommand;
searchCommand.CanExecute += CanExecuteRibbonCommand;
}
我已经试过了,不起作用,尽管它应该,至少在理论上。 – Vale 2011-04-07 13:53:16
你可以编辑问题发布XAML代码吗? – 2011-04-07 14:06:14
是的,但不是今天,我现在回家了,我明天一上班就会发布。 – Vale 2011-04-07 14:32:51