简体   繁体   English

WPF DataTemplateSelector 未使用

[英]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.xamlActivitiesView中被引用为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.

相关问题 WPF中的DataTemplateSelector - DataTemplateSelector in WPF WPF datatemplateselector没有被调用 - WPF datatemplateselector not getting called 在WPF中以编程方式将DataTemplateSelector与Treeview项绑定 - Binding DataTemplateSelector with treeview item programmatically in WPF WPF:当某个值发生变化时重新应用 DataTemplateSelector - WPF: Reapply DataTemplateSelector when a certain value changes 提供一个DataTemplateSelector运行时资源,如何? WPF / XAML - Supplying a DataTemplateSelector runtime resources, how? WPF/XAML 在DataTemplateSelector上使用DataTemplates的WPF字典的键始终为空值 - WPF Dictionary using DataTemplates on a DataTemplateSelector always having a null value for the key WPF-从DataTemplateSelector中获取一个DataGridCell的值 - WPF - Getting the value of one a DataGridCell from within DataTemplateSelector 如何在WPF中的DataTemplateSelector类中查找UserControl中的资源? - How to find a resource in a UserControl from a DataTemplateSelector class in WPF? LongListSelector和DataTemplateSelector - LongListSelector and DataTemplateSelector 引用父DataTemplateSelector的DataTemplateSelector中的DataTemplate? - DataTemplate within a DataTemplateSelector referencing the parent DataTemplateSelector?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM