在 WPF ControlTemplate 中查找控件
我创建了继承自 Window 的类,并将控件模板应用于它:
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在
OnApplyTemplate
中使用FindName
是正确的做法;我认为它不起作用,因为您忘记调用base.OnApplyTemplate()
。Using
FindName
inOnApplyTemplate
is the correct way of doing it; I think it doesn't work because you forgot to callbase.OnApplyTemplate()
.我敢打赌,您正在寻找一个不存在(或具有不同名称)或不是 RibbonCommand 的命令。
或者您没有为 xaml 中的命令指定
x:FieldModifier="protected"
。My bet is that you are looking for a command that doesn't exist (or has a different name) or isn't a RibbonCommand.
That or you didn't specify
x:FieldModifier="protected"
for the command in xaml.事实上,我犯了一个错误。当我尝试查找不是 RibbonCommands 的控件时,它起作用了,所以现在我首先找到父控件,然后使用 VisualTreeHelper 来查找 RibbonCommand。抱歉,我确信它只在扩展课程中有效,但我想当我发布问题时我太累了。不管怎样,谢谢你的帮助。
Actually, I made a mistake. When I try to find controls that are not RibbonCommands it worked, so now I find parent control first and than use VisualTreeHelper to find the RibbonCommand. Sorry about that, I was convinced that it worked only in extended class, but I guess I was too tired when I posted the question. Thank you for your help anyway.