[英]WPF DataTemplateSelector is not used
I am trying to modify a existing WPF application in a way so that a newer version of some data object can be used alongside the old one.我正在尝试以某种方式修改现有的 WPF 应用程序,以便某些数据 object 的较新版本可以与旧版本一起使用。 And so i avoid redundant code by extending the existing ViewModel with new fields where the old ones cannot be reused.
因此,我通过使用新字段扩展现有 ViewModel 来避免冗余代码,而旧字段不能被重用。
public IList<G1VU.PDR> VuG1 { get; set; }
public IList<G2VU.PDR> VuG2 { get; set; }
public PlacesCompound VuP
{
get
{
if (VuG1 != null && VuG2 == null)
{
return new PlacesCompound {
G1 = VuG1,
G2 = null
};
}
if (VuG2 != null && VuG1 == null)
{
return new PlacesCompound {
G1 = null,
G2 = VuG2
};
}
throw new Exception("G1 and G2 data present or no data present");
}
}
VuG1
has existed before and i have added a new property VuG2
for the new data. VuG1
之前已经存在,我为新数据添加了一个新属性VuG2
。 As you can see these are not the same class so i cannot interchange them.正如你所看到的,这些不是同一个 class 所以我不能互换它们。 For that reason i've added a property that will return either of the two in a
PlacesCompound
class, which is just a class with two properties and nothing else.出于这个原因,我添加了一个属性,它将在
PlacesCompound
class 中返回两者中的任何一个,这只是一个具有两个属性的 class,仅此而已。
In the corresponding usercontrol (lets call it ActivitiesView ) we have a DataGrid which binds to the ViewModel and somewhere a TabItem that will display a custom UserControl places
which binds to VuG1
on the ViewModel.在相应的用户控件(我们称之为ActivitiesView )中,我们有一个绑定到 ViewModel 的 DataGrid 和一个 TabItem,它将显示一个自定义的 UserControl
places
,该位置绑定到 ViewModel 上的VuG1
。 I have copied it and changed it so it will work with VuG2
Data.我已经复制并更改了它,以便它可以与
VuG2
数据一起使用。 And i created a custom DataTemplateSelector
which will decide what Template to use based on which variable of PlacesCompound
isnt null
.我创建了一个自定义
DataTemplateSelector
,它将根据null
PlacesCompound
决定使用什么模板。
In VUActivitiesResources.xaml
i have then declared 2 DataTemplates one for each places
UserControl and the DataTemplateSelector.在
VUActivitiesResources.xaml
我为每个places
UserControl 和 DataTemplateSelector 声明了 2 个 DataTemplates。
<activities:VUActivitiesViewDataTemplateSelector x:Key="PlacesTemplateSelector"/>
<DataTemplate x:Key="VuG2Template">
<places:VUPViewG2 DataContext="{Binding VuG2}" HorizontalAlignment="Left"/>
</DataTemplate>
<DataTemplate x:Key="VuG1Template">
<places:VUPViewG1 DataContext="{Binding VuG1}" HorizontalAlignment="Left"/>
</DataTemplate>
VUActivitiesResources.xaml
is being referenced in ActivitiesView as UserControl.Resources
. VUActivitiesResources.xaml
在ActivitiesView中被引用为UserControl.Resources
。
In the ActivitiesView i placed a ItemsControl into the TabItem replacing the custom places
UserControl (ive also tried a ListBox instead of a ItemsControl, but neither works)在活动视图中,我将 ItemsControl 放入 TabItem 中,替换了自定义
places
UserControl(我也尝试了 ListBox 而不是 ItemsControl,但都不起作用)
<TabItem IsEnabled="{Binding PlacesIsVisible}">
...
<ItemsControl
ItemsSource="{Binding VuP}"
ItemTemplateSelector="{StaticResource PlacesTemplateSelector}"></ItemsControl>
</TabItem>
My question: why PlacesTemplateSelector
is never used and how do i make it being used?我的问题:为什么
PlacesTemplateSelector
从未使用过,我如何让它被使用? Because right now while debugging i can see that in ViewModel VuP
returns a PlacesCompound
object correctly but the Selector is never entered.因为现在在调试时我可以看到在 ViewModel
VuP
中正确返回了PlacesCompound
object 但从未输入选择器。 I want one of the two DataTemplates to show up in the TabItem and right now none is showing.我希望两个 DataTemplate 之一显示在 TabItem 中,现在没有显示。
ItemsSource must be a collection (which your PlacesCompound is not), in your case this would be either VuG1 or VuG2. ItemsSource 必须是一个集合(您的 PlacesCompound 不是),在您的情况下,这将是 VuG1 或 VuG2。 If the two item classes have no common base class, you can still use
IEnumerable
:如果两个项目类没有共同的基础 class,您仍然可以使用
IEnumerable
:
public IEnumerable VuP => (IEnumerable)VuG1 ?? VuG2;
Then, instead of writing a TemplateSelector, just let the WPF DataTemplating mechanism do its work:然后,不用编写 TemplateSelector,只需让 WPF DataTemplating 机制完成其工作:
<DataTemplate DataType="{x:Type namespace1:PDR}">
<places:VUPViewG2 HorizontalAlignment="Left"/>
</DataTemplate>
<DataTemplate DataType="{x:Type namespace2:PDR}">
<places:VUPViewG1 HorizontalAlignment="Left"/>
</DataTemplate>
<ItemsControl ItemsSource="{Binding VuP}" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.