WPF:从 DataTemplateSelector 类中的 UserControl 查找资源

发布于 2024-10-07 07:39:32 字数 610 浏览 8 评论 0原文

我知道有这个线程: 如何从 WPF 中的 DataTemplateSelector 类中查找 UserControl 中的资源?

问同样的问题。

但是...我对答案不满意!必须有一种方法来获取

包含 ContentControl/Presenter 声明的 UserControl 的资源:

ContentTemplateSelector="{StaticResource MySelector}" 

每个派生的 DataTemplateSelector 类在其 SelectedTemplate 方法 => 中都有一个参数

DependencyObject 类型的容器。

在我的例子中,容器就是内容控件。

是否不可能从“contentcontrol”开始爬上视觉树并尝试 通过 FindAncestor 获取 UserControl ?

I know there is this thread: How to find a resource in a UserControl from a DataTemplateSelector class in WPF?

asking the same.

BUT... I am not satisfied with the answer! THERE MUST be a way to grab the Resources of the

UserControl containing the ContentControl/Presenter declaring this:

ContentTemplateSelector="{StaticResource MySelector}" 

Each derived DataTemplateSelector class a parameter in its SelectedTemplate Method =>

container which is typeof DependencyObject.

Well container is in my case the contentcontrol.

Would it not be possible to climb up the visual tree starting at "contentcontrol" and try
to get the UserControl via FindAncestor ?

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

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

发布评论

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

评论(1

诗笺 2024-10-14 07:39:32

是的,您可以将 container 参数强制转换为 FrameworkElement 并调用 FindResourceContentPresenter 开始进行资源查找。例如:

代码:

public class MySelector
    : DataTemplateSelector
{
    public override DataTemplate SelectTemplate
        (object item, DependencyObject container)
    {
        // Determine the resource key to use
        var key = item.ToString() == "a" ? "one" : "two";
        // Find the resource starting from the container
        return ((FrameworkElement)container).FindResource(key) as DataTemplate;
    }
}

XAML:

<UserControl
    x:Class="WpfApplication1.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    >
    <UserControl.Resources>
        <DataTemplate x:Key="one">
            <TextBlock>Template One</TextBlock>
        </DataTemplate>
        <DataTemplate x:Key="two">
            <TextBlock>Template Two</TextBlock>
        </DataTemplate>
        <local:MySelector x:Key="MySelector"/>
    </UserControl.Resources>
    <StackPanel>
        <ContentPresenter
            ContentTemplateSelector="{StaticResource MySelector}"
            Content="a"/>
        <ContentPresenter
            ContentTemplateSelector="{StaticResource MySelector}"
            Content="b"/>
    </StackPanel>
</UserControl>

Yes, you can cast the container parameter to FrameworkElement and call FindResource to do a resource lookup starting at the ContentPresenter. For example:

Code:

public class MySelector
    : DataTemplateSelector
{
    public override DataTemplate SelectTemplate
        (object item, DependencyObject container)
    {
        // Determine the resource key to use
        var key = item.ToString() == "a" ? "one" : "two";
        // Find the resource starting from the container
        return ((FrameworkElement)container).FindResource(key) as DataTemplate;
    }
}

XAML:

<UserControl
    x:Class="WpfApplication1.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    >
    <UserControl.Resources>
        <DataTemplate x:Key="one">
            <TextBlock>Template One</TextBlock>
        </DataTemplate>
        <DataTemplate x:Key="two">
            <TextBlock>Template Two</TextBlock>
        </DataTemplate>
        <local:MySelector x:Key="MySelector"/>
    </UserControl.Resources>
    <StackPanel>
        <ContentPresenter
            ContentTemplateSelector="{StaticResource MySelector}"
            Content="a"/>
        <ContentPresenter
            ContentTemplateSelector="{StaticResource MySelector}"
            Content="b"/>
    </StackPanel>
</UserControl>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文