我正在创建自己的UserControl,我在XAML的 UserControl.Resources 部分下有两个不同的DataTemplates。我想在这两个datatemplates之间进行选择,具体取决于listview中显示的对象的属性值。我这样做是通过创建一个自定义的 DataTemplateSelector 类并重写 SelectTemplate 方法,该方法应该返回我想要使用的DataTemplate。但是,我不知道如何“找到”位于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;
}
答案 0 :(得分:7)
试试这个:
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;
}
答案 1 :(得分:6)
我通常使用DataConmplateSelector的构造函数中的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();
}
}
不是镇上最漂亮的女孩,但它完成了工作;)
希望这有帮助!
答案 2 :(得分: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!
答案 3 :(得分:0)
WinRT&amp; 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方法基于此处接受的答案:How to get a ListView from a ListViewItem?