5

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


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 Property

4

4 回答 4

8

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

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();
    }
}

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

希望这可以帮助!

于 2009-05-27T14:31:45.270 回答
8

试试这个:

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        if (item != null && item is AuctionItem)
        {
            AuctionItem auctionItem = item as AuctionItem;

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

        return null;
    }
于 2011-08-17T13:20:56.283 回答
2
       <DataTemplate x:Key="addTemplate">
        <Button Command="{Binding Path=AddCommand}">Add</Button>
    </DataTemplate>

    <DataTemplate x:Key="editTemplate">
        <Button Command="{Binding Path=UpdateCommand}">Update</Button>
    </DataTemplate>

    <TemplateSelectors:AddEditTemplateSelector
        AddTemplate="{StaticResource addTemplate}"
        EditTemplate="{StaticResource editTemplate}"
        x:Key="addEditTemplateSelector" />

仅限 XAML!

于 2010-03-25T22:16:47.763 回答
0

WinRT 和 Windows Phone 解决方案涉及向上移动可视化树,直到找到父控件:

    protected override Windows.UI.Xaml.DataTemplate SelectTemplateCore(object item, Windows.UI.Xaml.DependencyObject container)
    {
        var parent = FindParent<MyParentControlType>(container as FrameworkElement);

        if(parent != null)
        {
            if (item is Something)
                return parent.Resources["TemplateForSomething"] as DataTemplate;
            else if(item is SomethingElse)
                return parent.Resources["TemplateForSomethingElse"] as DataTemplate;
            else 
                // etc
        }
        else
        {
            return App.Current.Resources["SomeFallbackResource"] as DataTemplate;
        }
    }

    public static T FindParent<T>(FrameworkElement element) where T : FrameworkElement
    {
        FrameworkElement parent = Windows.UI.Xaml.Media.VisualTreeHelper.GetParent(element) as FrameworkElement;

        while (parent != null)
        {
            T correctlyTyped = parent as T;

            if (correctlyTyped != null)
                return correctlyTyped;
            else
                return FindParent<T>(parent);
        }

        return null;
    }

FindParent 方法基于此处接受的答案:如何从 ListViewItem 获取 ListView?

于 2014-08-20T13:52:38.740 回答