在 WPF ControlTemplate 中查找控件

发布于 2024-10-31 03:53:29 字数 1311 浏览 5 评论 0原文

我创建了继承自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

_蜘蛛 2024-11-07 03:53:29

OnApplyTemplate 中使用 FindName 是正确的做法;我认为它不起作用,因为您忘记调用base.OnApplyTemplate()

Using FindName in OnApplyTemplate is the correct way of doing it; I think it doesn't work because you forgot to call base.OnApplyTemplate().

甜嗑 2024-11-07 03:53:29

我敢打赌,您正在寻找一个不存在(或具有不同名称)或不是 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.

自此以后,行同陌路 2024-11-07 03:53:29

事实上,我犯了一个错误。当我尝试查找不是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文