如何从WPF中的DataTemplateSelector类在UserControl中查找资源? [英] How to find a resource in a UserControl from a DataTemplateSelector class in WPF?

查看:42
本文介绍了如何从WPF中的DataTemplateSelector类在UserControl中查找资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建自己的UserControl,并且在XAML的 UserControl.Resources 部分下有两个不同的DataTemplates.我想根据在列表视图中显示的对象的属性值在这两个数据模板之间进行选择.我通过创建一个自定义的 DataTemplateSelector 类并覆盖 SelectTemplate 方法来实现此目的,该方法应该返回我要使用的DataTemplate.但是,我不知道如何查找"位于UserControls资源部分的数据模板,我所看到的所有示例仅从 Window.Resources 获取数据模板.在此示例中,他们获取当前的 MainWindow ,然后使用 FindResource 查找 DataTemplate ,如何获取我的 UserControl 以类似的方式进行?:

I'm creating my own UserControl and I have two different DataTemplates under the UserControl.Resources section in my XAML. I want to choose between these two datatemplates depending on the value of a property on objects displayed in a listview. I do this by creating a custom DataTemplateSelector class and overriding the SelectTemplate method which is supposed to return the DataTemplate I wish to use. However, I have no idea how to "find" my datatemplates that are located in the UserControls resource section, all the examples I've seen only fetches datatemplates from Window.Resources. In this example they fetch the current MainWindow and then use FindResource to find the DataTemplate, how do I fetch my UserControl in a similar manner?:


public override DataTemplate 
            SelectTemplate(object item, DependencyObject container)
        {
            if (item != null && item is AuctionItem)
            {
                AuctionItem auctionItem = item as AuctionItem;
                Window window = Application.Current.MainWindow;

                switch (auctionItem.SpecialFeatures)
                {
                    case SpecialFeatures.None:
                        return 
                            window.FindResource("AuctionItem_None") 
                            as DataTemplate;
                    case SpecialFeatures.Color:
                        return 
                            window.FindResource("AuctionItem_Color") 
                            as DataTemplate;
                }
            }

            return null;
        }

上面的示例来自此处:ItemsControl.ItemTemplateSelector属性

The example above is from here: ItemsControl.ItemTemplateSelector Property

推荐答案

我通常使用DataControlSelector构造函数中的UserControl作为参数从后面的代码实例化DataTemplateSelector,如下所示:

I usually instantiate my DataTemplateSelector from code behind with the UserControl as parameter in the constructor of the DataTemplateSelector, like so:

public class MyUserControl : UserControl
{
    public MyUserControl()
    {
        Resources["MyDataTemplateSelector"] = new MyDataTemplateSelector(this);
        InitializeComponent();
    }
}

public class MyDataTemplateSelector : DataTemplateSelector
{
    private MyUserControl parent;
    public MyDataTemplateSelector(MyUserControl parent)
    {
        this.parent = parent;
    }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        parent.DoStuff();
    }
}

这不是镇上最漂亮的女孩,但它能完成工作;)

Not the most prettiest girl in town, but it get the job done ;)

希望这会有所帮助!

这篇关于如何从WPF中的DataTemplateSelector类在UserControl中查找资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆