未调用 ItemsControl 派生类上的 WPF OnApplyTemplate

发布于 2024-11-12 05:07:47 字数 1292 浏览 2 评论 0原文

标题基本上已经指出了这一点。我读过与同一问题相关的其他博客和帖子,但所提供的解决方案都不适合我。

这是我的代码的简化:

<!-- CustomItemsControl.xaml -->
<ItemsControl x:Class="myNamespace.CustomItemsControl"
              xmlns:local="clr-namespace:myNamespace">

    <ItemsControl.Resources>    
        <Style TargetType="{x:Type local:CustomItemsControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:CustomItemsControl}">
                        <Grid x:Name="MyItemsHost" Background="{TemplateBinding Background}" IsItemsHost="True"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ItemsControl.Resources>
</ItemsControl>

// CustomItemsControl.xaml.cs
namespace myNamespace
{
    public partial class CustomItemsControl : ItemsControl
    {
        public CustomItemsControl()
        {
            this.DefaultStyleKey = typeof(CustomItemsControl);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            var MyItemsHost = (Grid)base.GetTemplateChild("MyItemsHost");
        }
    }
}

我做错了什么?

The title basically points it out. I've read other blogs and posts related to the same issue, but none of the provided solutions did work for me.

Here's a simplification of my code:

<!-- CustomItemsControl.xaml -->
<ItemsControl x:Class="myNamespace.CustomItemsControl"
              xmlns:local="clr-namespace:myNamespace">

    <ItemsControl.Resources>    
        <Style TargetType="{x:Type local:CustomItemsControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:CustomItemsControl}">
                        <Grid x:Name="MyItemsHost" Background="{TemplateBinding Background}" IsItemsHost="True"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ItemsControl.Resources>
</ItemsControl>

// CustomItemsControl.xaml.cs
namespace myNamespace
{
    public partial class CustomItemsControl : ItemsControl
    {
        public CustomItemsControl()
        {
            this.DefaultStyleKey = typeof(CustomItemsControl);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            var MyItemsHost = (Grid)base.GetTemplateChild("MyItemsHost");
        }
    }
}

What I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

尸血腥色 2024-11-19 05:07:47

我以前没有见过这样的做法,我在 Generic.xaml 中定义了模板,因为这是当您转到“项目”->“添加自定义控件 (WPF)”时 Visual Studio 生成的模板。

您可以通过在构造函数中调用 InitializeComponent(); 来使您发布的代码正常工作。
文档还说您应该使用 Template.FindName("MyItemsHost",this) 而不是 GetTemplateChild。如果您的控件可能需要除网格之外的不同布局,您可能需要使用 ItemsPresenter 并设置 ItemsPanelTemplate。

I haven't seen it done that way before, I define the template in Generic.xaml as that is what Visual Studio generates when you go Project->Add Custom Control (WPF).

You can make the code you posted work by calling InitializeComponent(); in your constructor.
Also the documentation says you should use Template.FindName("MyItemsHost",this) instead of GetTemplateChild. If your control may need a different layout other than the grid you may want to use an ItemsPresenter and set the ItemsPanelTemplate instead.

盛装女皇 2024-11-19 05:07:47

根据您的自定义控件的派生来源,您可能无法对其调用 InitializeComponent()。例如,ContentControl 不提供InitializeComponent。

如果您检查线程中,您会看到 OnApplyTemplate 从未被调用的原因是因为您将项目定义为类库,而不是自定义控件库。 Visual Studio 向 AssemblyInfo.cs 添加额外信息,以告诉运行时在哪里可以找到控件的模板。

如果将以下代码添加到 AssemblyInfo.cs 文件中,它应该开始正常运行:)

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page, 
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page, 
// app, or any theme specific resource dictionaries)

]

Depending on what your Custom Control derives from, you may not be able to call InitializeComponent() on it. For example, a ContentControl doesn't provide InitializeComponent.

If you check this thread, you'll see that the reason OnApplyTemplate never gets called, is because you defined the project as a Class Library, not a Custom Control Library. Visual Studio adds extra information to AssemblyInfo.cs to tell the runtime where to find the template for your control.

If you add the following code to your AssemblyInfo.cs file, it should start behaving correctly:

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page, 
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page, 
// app, or any theme specific resource dictionaries)

)]

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