UWP 从 TemplateSelector 更改为用户控件不再显示绑定

Posted

技术标签:

【中文标题】UWP 从 TemplateSelector 更改为用户控件不再显示绑定【英文标题】:UWP Changing from TemplateSelector to user Control does not show binding anymore 【发布时间】:2020-08-20 11:16:45 【问题描述】:

按照 Richard Zhang - MSFT 在我的问题UWP Treeview with Data selector generating an error in App.g.cs when I resize the window 中提供的工作解决方案,我尝试将第二组数据模板转换为控件以获得更清晰的代码并尝试解决其他一些可视化问题。 代码可以编译,但没有显示任何内容。

使用 TreeView 模板选择器的工作代码:

    <Page.Resources>
        <DataTemplate
            x:Key="ItemTemplate"
            x:DataType="model:MTreeViewBase"
            x:DefaultBindMode="OneWay">
            <winui:TreeViewItem IsExpanded="False" ItemsSource="x:Bind Visits">
                <controls1:TreeViewControl Data="x:Bind" />
            </winui:TreeViewItem>
        </DataTemplate>

        <DataTemplate
            x:Key="PAZContentTemplate"
            x:DataType="model:MTreeViewPaz"
            x:DefaultBindMode="OneWay">
            <StackPanel>
                <TextBlock Style="StaticResource TitleTextBlockStyle" Text="x:Bind Name" />
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <TextBlock
                        Grid.Row="0"
                        Grid.Column="0"
                        Margin="StaticResource MediumTopMargin"
                        Style="StaticResource DetailSubTitleStyle"
                        Text="Cognome" />
                    <TextBlock
                        Grid.Row="1"
                        Grid.Column="0"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text="x:Bind Paz.Cognome" />
                    <TextBlock
                        Grid.Row="0"
                        Grid.Column="1"
                        Margin="StaticResource MediumTopMargin"
                        Style="StaticResource DetailSubTitleStyle"
                        Text="Nome" />
                    <TextBlock
                        Grid.Row="1"
                        Grid.Column="1"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text="x:Bind Paz.Nome" />
                </Grid>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <TextBlock
                        Grid.Row="0"
                        Grid.Column="0"
                        Margin="StaticResource MediumTopMargin"
                        Style="StaticResource DetailSubTitleStyle"
                        Text="Telefono 1" />
                    <TextBlock
                        Grid.Row="1"
                        Grid.Column="0"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text="x:Bind Paz.Telef1" />
                    <TextBlock
                        Grid.Row="0"
                        Grid.Column="1"
                        Margin="StaticResource MediumTopMargin"
                        Style="StaticResource DetailSubTitleStyle"
                        Text="Telefono 2" />
                    <TextBlock
                        Grid.Row="1"
                        Grid.Column="1"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text="x:Bind Paz.Telef2" />
                    <TextBlock
                        Grid.Row="2"
                        Grid.Column="0"
                        Margin="StaticResource XSmallTopMargin"
                        Style="StaticResource DetailSubTitleStyle"
                        Text="Fax" />
                    <TextBlock
                        Grid.Row="3"
                        Grid.Column="0"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text="x:Bind Paz.Fax" />
                </Grid>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <TextBlock
                        Grid.Row="0"
                        Grid.Column="0"
                        Grid.ColumnSpan="5"
                        Margin="StaticResource MediumTopMargin"
                        Style="StaticResource DetailSubTitleStyle"
                        Text="Indirizzo" />
                    <TextBlock
                        Grid.Row="1"
                        Grid.Column="0"
                        Grid.ColumnSpan="5"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text="x:Bind Paz.Indir" />

                    <TextBlock
                        Grid.Row="2"
                        Grid.Column="0"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text="x:Bind Paz.Cap" />
                    <TextBlock
                        Grid.Row="2"
                        Grid.Column="1"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text="x:Bind Paz.Citta" />
                    <TextBlock
                        Grid.Row="2"
                        Grid.Column="2"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text=" (" />
                    <TextBlock
                        Grid.Row="2"
                        Grid.Column="3"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text="x:Bind Paz.Cap" />
                    <TextBlock
                        Grid.Row="2"
                        Grid.Column="4"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text=")" />
                    <TextBlock
                        Grid.Row="3"
                        Grid.Column="0"
                        Grid.ColumnSpan="5"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text="x:Bind Paz.Nazione" />

                </Grid>
                <TextBlock
                    Margin="StaticResource MediumTopMargin"
                    Style="StaticResource DetailSubTitleStyle"
                    Text="Codice Fiscale" />
                <TextBlock Style="StaticResource DetailBodyBaseMediumStyle" Text="x:Bind Paz.CodFis" />

                <TextBlock
                    Margin="StaticResource MediumTopMargin"
                    Style="StaticResource DetailSubTitleStyle"
                    Text="Nato il " />
                <TextBlock Style="StaticResource DetailBodyBaseMediumStyle" Text="x:Bind BornDate" />
                <TextBlock
                    Margin="StaticResource XSmallTopMargin"
                    Style="StaticResource DetailSubTitleStyle"
                    Text="in" />
                <TextBlock Style="StaticResource DetailBodyBaseMediumStyle" Text="x:Bind Paz.Naton" />
                <StackPanel Orientation="Horizontal">
                    <TextBlock Style="StaticResource DetailBodyBaseMediumStyle" Text="x:Bind Paz.Natoa" />
                    <TextBlock Text=" (" />
                    <TextBlock Style="StaticResource DetailBodyBaseMediumStyle" Text="x:Bind Paz.Natop" />
                    <TextBlock Text=")" />
                </StackPanel>

                <TextBlock
                    Margin="StaticResource MediumTopMargin"
                    Style="StaticResource DetailSubTitleStyle"
                    Text="Clinica" />
                <TextBlock Style="StaticResource DetailBodyBaseMediumStyle" Text="x:Bind Paz.Clinica" />

                <TextBlock
                    Margin="StaticResource MediumTopMargin"
                    Style="StaticResource DetailSubTitleStyle"
                    Text="Presentato da" />
                <TextBlock Style="StaticResource DetailBodyBaseMediumStyle" Text="x:Bind Paz.PresDa" />

                <TextBlock
                    Margin="StaticResource MediumTopMargin"
                    Style="StaticResource DetailSubTitleStyle"
                    Text="Assicurazione" />
                <TextBlock Style="StaticResource DetailBodyBaseMediumStyle" Text="x:Bind Paz.Assicurazione" />
            </StackPanel>
        </DataTemplate>
        <DataTemplate
            x:Key="APPContentTemplate"
            x:DataType="model:MTreeViewVisit"
            x:DefaultBindMode="OneWay">
            <StackPanel>
                <TextBlock
                    Foreground="x:Bind ImageColor"
                    Style="StaticResource TitleTextBlockStyle"
                    Text="Analisi Patologia Prossima" />
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <TextBlock
                        Grid.Row="0"
                        Grid.Column="0"
                        Margin="StaticResource MediumTopMargin"
                        Style="StaticResource DetailSubTitleStyle"
                        Text="Clinica" />
                    <TextBlock
                        Grid.Row="1"
                        Grid.Column="0"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text="x:Bind Visit.(model:TApp.Clinica)" />
                    <TextBlock
                        Grid.Row="0"
                        Grid.Column="1"
                        Margin="StaticResource MediumLeftTopMargin"
                        Style="StaticResource DetailSubTitleStyle"
                        Text="Data" />
                    <TextBlock
                        Grid.Row="1"
                        Grid.Column="1"
                        Margin="StaticResource MediumLeftMargin"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text="x:Bind Visit.(model:TApp.DataS)" />
                </Grid>

                <TextBlock
                    Margin="StaticResource MediumTopMargin"
                    Style="StaticResource DetailSubTitleStyle"
                    Text="Descrizione" />
                <TextBlock Style="StaticResource DetailBodyBaseMediumStyle" Text="x:Bind Visit.(model:TApp.Descr)" />
            </StackPanel>
        </DataTemplate>
    </Page.Resources>

        <templateSelectors:TreeViewDataContentTemplateSelector
            x:Key="TreeViewDataContentTemplateSelector"
            APPTemplate="StaticResource APPContentTemplate"
            PAZTemplate="StaticResource PAZContentTemplate" />

        <ScrollViewer Grid.Column="1" Padding="StaticResource DetailPageMargin" x:DefaultBindMode="OneWay">
            <ContentControl Content="x:Bind SelectedItem" ContentTemplateSelector="StaticResource TreeViewDataContentTemplateSelector" />
        </ScrollViewer>

当我单击 TreeView 中的项目时,面板会显示正确的信息。

但是如果我尝试这个:

TrewView.xaml

       <ScrollViewer Grid.Column="1" Padding="StaticResource DetailPageMargin" >
            <controls1:TreeViewContentControl Data="x:Bind (model:MTreeViewBase)SelectedItem, Mode=OneWay"    />
        </ScrollViewer>

TreeViewContentControl.xaml

<UserControl
    x:Class="TitoDoc2020.Views.TreeViewContentControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TitoDoc2020.Views"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:model="using:TitoDoc2020.Models"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400">

    <UserControl.Resources>
        <DataTemplate
            x:Name="PAZContentTemplate"
            x:DataType="model:MTreeViewPaz"
            x:DefaultBindMode="OneWay">
            <StackPanel>
                <TextBlock Style="StaticResource TitleTextBlockStyle" Text="x:Bind Name" />
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <TextBlock
                        Grid.Row="0"
                        Grid.Column="0"
                        Margin="StaticResource MediumTopMargin"
                        Style="StaticResource DetailSubTitleStyle"
                        Text="Cognome" />
                    <TextBlock
                        Grid.Row="1"
                        Grid.Column="0"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text="x:Bind Paz.Cognome" />
                    <TextBlock
                        Grid.Row="0"
                        Grid.Column="1"
                        Margin="StaticResource MediumTopMargin"
                        Style="StaticResource DetailSubTitleStyle"
                        Text="Nome" />
                    <TextBlock
                        Grid.Row="1"
                        Grid.Column="1"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text="x:Bind Paz.Nome" />
                </Grid>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <TextBlock
                        Grid.Row="0"
                        Grid.Column="0"
                        Margin="StaticResource MediumTopMargin"
                        Style="StaticResource DetailSubTitleStyle"
                        Text="Telefono 1" />
                    <TextBlock
                        Grid.Row="1"
                        Grid.Column="0"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text="x:Bind Paz.Telef1" />
                    <TextBlock
                        Grid.Row="0"
                        Grid.Column="1"
                        Margin="StaticResource MediumTopMargin"
                        Style="StaticResource DetailSubTitleStyle"
                        Text="Telefono 2" />
                    <TextBlock
                        Grid.Row="1"
                        Grid.Column="1"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text="x:Bind Paz.Telef2" />
                    <TextBlock
                        Grid.Row="2"
                        Grid.Column="0"
                        Margin="StaticResource XSmallTopMargin"
                        Style="StaticResource DetailSubTitleStyle"
                        Text="Fax" />
                    <TextBlock
                        Grid.Row="3"
                        Grid.Column="0"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text="x:Bind Paz.Fax" />
                </Grid>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <TextBlock
                        Grid.Row="0"
                        Grid.Column="0"
                        Grid.ColumnSpan="5"
                        Margin="StaticResource MediumTopMargin"
                        Style="StaticResource DetailSubTitleStyle"
                        Text="Indirizzo" />
                    <TextBlock
                        Grid.Row="1"
                        Grid.Column="0"
                        Grid.ColumnSpan="5"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text="x:Bind Paz.Indir" />

                    <TextBlock
                        Grid.Row="2"
                        Grid.Column="0"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text="x:Bind Paz.Cap" />
                    <TextBlock
                        Grid.Row="2"
                        Grid.Column="1"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text="x:Bind Paz.Citta" />
                    <TextBlock
                        Grid.Row="2"
                        Grid.Column="2"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text=" (" />
                    <TextBlock
                        Grid.Row="2"
                        Grid.Column="3"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text="x:Bind Paz.Cap" />
                    <TextBlock
                        Grid.Row="2"
                        Grid.Column="4"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text=")" />
                    <TextBlock
                        Grid.Row="3"
                        Grid.Column="0"
                        Grid.ColumnSpan="5"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text="x:Bind Paz.Nazione" />

                </Grid>
                <TextBlock
                    Margin="StaticResource MediumTopMargin"
                    Style="StaticResource DetailSubTitleStyle"
                    Text="Codice Fiscale" />
                <TextBlock Style="StaticResource DetailBodyBaseMediumStyle" Text="x:Bind Paz.CodFis" />

                <TextBlock
                    Margin="StaticResource MediumTopMargin"
                    Style="StaticResource DetailSubTitleStyle"
                    Text="Nato il " />
                <TextBlock Style="StaticResource DetailBodyBaseMediumStyle" Text="x:Bind BornDate" />
                <TextBlock
                    Margin="StaticResource XSmallTopMargin"
                    Style="StaticResource DetailSubTitleStyle"
                    Text="in" />
                <TextBlock Style="StaticResource DetailBodyBaseMediumStyle" Text="x:Bind Paz.Naton" />
                <StackPanel Orientation="Horizontal">
                    <TextBlock Style="StaticResource DetailBodyBaseMediumStyle" Text="x:Bind Paz.Natoa" />
                    <TextBlock Text=" (" />
                    <TextBlock Style="StaticResource DetailBodyBaseMediumStyle" Text="x:Bind Paz.Natop" />
                    <TextBlock Text=")" />
                </StackPanel>

                <TextBlock
                    Margin="StaticResource MediumTopMargin"
                    Style="StaticResource DetailSubTitleStyle"
                    Text="Clinica" />
                <TextBlock Style="StaticResource DetailBodyBaseMediumStyle" Text="x:Bind Paz.Clinica" />

                <TextBlock
                    Margin="StaticResource MediumTopMargin"
                    Style="StaticResource DetailSubTitleStyle"
                    Text="Presentato da" />
                <TextBlock Style="StaticResource DetailBodyBaseMediumStyle" Text="x:Bind Paz.PresDa" />

                <TextBlock
                    Margin="StaticResource MediumTopMargin"
                    Style="StaticResource DetailSubTitleStyle"
                    Text="Assicurazione" />
                <TextBlock Style="StaticResource DetailBodyBaseMediumStyle" Text="x:Bind Paz.Assicurazione" />
            </StackPanel>
        </DataTemplate>

        <DataTemplate
            x:Name="APPContentTemplate"
            x:DataType="model:MTreeViewVisit"
            x:DefaultBindMode="OneWay">
            <StackPanel>
                <TextBlock
                    Foreground="x:Bind ImageColor"
                    Style="StaticResource TitleTextBlockStyle"
                    Text="Analisi Patologia Prossima" />
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <TextBlock
                        Grid.Row="0"
                        Grid.Column="0"
                        Margin="StaticResource MediumTopMargin"
                        Style="StaticResource DetailSubTitleStyle"
                        Text="Clinica" />
                    <TextBlock
                        Grid.Row="1"
                        Grid.Column="0"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text="x:Bind Visit.(model:TApp.Clinica)" />
                    <TextBlock
                        Grid.Row="0"
                        Grid.Column="1"
                        Margin="StaticResource MediumLeftTopMargin"
                        Style="StaticResource DetailSubTitleStyle"
                        Text="Data" />
                    <TextBlock
                        Grid.Row="1"
                        Grid.Column="1"
                        Margin="StaticResource MediumLeftMargin"
                        Style="StaticResource DetailBodyBaseMediumStyle"
                        Text="x:Bind Visit.(model:TApp.DataS)" />
                </Grid>

                <TextBlock
                    Margin="StaticResource MediumTopMargin"
                    Style="StaticResource DetailSubTitleStyle"
                    Text="Descrizione" />
                <TextBlock Style="StaticResource DetailBodyBaseMediumStyle" Text="x:Bind Visit.(model:TApp.Descr)" />
            </StackPanel>
        </DataTemplate>
    </UserControl.Resources>

    <Grid>
        <ContentControl x:Name="MainContent" />
    </Grid>
</UserControl>

TreeViewContentControl.xaml.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using TitoDoc2020.Models;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236

namespace TitoDoc2020.Views

    public sealed partial class TreeViewContentControl : UserControl
    
#if LOG
        private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
#endif
        public MTreeViewBase Data
        
            get  return (MTreeViewBase)GetValue(DataProperty); 
            set  SetValue(DataProperty, value); 
        

        // Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DataProperty =
            DependencyProperty.Register("Data", typeof(MTreeViewBase), typeof(TreeViewContentControl), new PropertyMetadata(null, new PropertyChangedCallback(Data_Changed)));

        private static void Data_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        
            if (e.NewValue != null)
            
                var instance = d as TreeViewContentControl;
                if (e.NewValue is MTreeViewPaz)
                
                    instance.MainContent.ContentTemplate = instance.PAZContentTemplate;
                
                else
                
                    //instance.MainContent.ContentTemplate = instance.VisitTemplate;
                    switch (((MTreeViewVisit)e.NewValue).Type)
                    
                        // Generic Visits
                        case "APP":
                            instance.MainContent.ContentTemplate = instance.APPContentTemplate;
                            break;
                        default:
                            if (e.NewValue != null)
                            
#if LOG
                                Logger.Error("Should never be default");
#endif
                            
                            break;
                    
                

            
        
        public TreeViewContentControl()
        
            this.InitializeComponent();
        
    

它不显示 x:Bind 中的任何内容。页面更改正确,显示的是静态文本,但不是真实数据。

我认为我所做的与其他控件完全相同,但无法理解我搞砸了什么

【问题讨论】:

你好,如果MTreeViewBase是一个基类,而你传入的数据是从这个类派生的,那么你就不用在绑定语句中添加转换语句(model: MTreeViewBase)了。还要检查包含SelectItem 变量的父类是否实现了 INotifyPropertyChanged 接口。 嗨,Richard,父类是 TreeView 页面:public sealed partial class TitoDocPage : Page, INotifyPropertyChanged private MTreeViewBase _selectedItem; public MTreeViewBase SelectedItem get return _selectedItem; set Set(ref _selectedItem, value); public event PropertyChangedEventHandler PropertyChanged; private void Set&lt;T&gt;(ref T storage, T value, [CallerMemberName]string propertyName = null) if (Equals(storage, value)) return; storage = value; OnPropertyChanged(propertyName); private void OnPropertyChanged(string propertyName) =&gt; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 所以我想说我对这门课很好。即使您建议进行更改,我也没有更新 另外,Richard 请注意,当我单击一个项目时,右侧的页面会相应更改(从 PAZ 到 IMG 到 PLP 等...),但未显示数据。因此,每次都使用正确的项目调用 private static void Data_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e),但数据模板中的 x:Bind 不起作用。例如:&lt;TextBlock Style="StaticResource TitleTextBlockStyle" Text="x:Bind Name" /&gt; 但我没有任何错误,如果在 VS2019 中我右键单击名称并选择 go to definition 菜单,它会正确解析并打开正确的项目。 【参考方案1】:

感谢Peter Spada 和他的惊人帮助,我才能让它工作:

问题出在这里:我将 instance.MainContent.ContentTemplate 设置为带有 x:DataType=”model:MTreeViewPaz” 的 DataTemplate。但是,instance.MainContent 从未设置过ContentDataContext,因此它使用默认的DataContext。默认DataContext继承自XAML树,来自根ShellPage

这就是InvalidCastException 无法将ShellPage 转换为MTreeViewPaz 的原因。

我可以通过手动设置 DataContext 来修复它,如下所示。

TreeViewContentControl.xaml.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using TitoDoc2020.Models;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236

namespace TitoDoc2020.Views

    public sealed partial class TreeViewContentControl : UserControl
    
#if LOG
        private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
#endif
        public MTreeViewBase Data
        
            get  return (MTreeViewBase)GetValue(DataProperty); 
            set  SetValue(DataProperty, value); 
        

        // Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DataProperty =
            DependencyProperty.Register("Data", typeof(MTreeViewBase), typeof(TreeViewContentControl), new PropertyMetadata(null, new PropertyChangedCallback(Data_Changed)));

        private static void Data_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        
            if (e.NewValue != null)
            
                var instance = d as TreeViewContentControl;
                if (e.NewValue is MTreeViewPaz)
                
                    instance.MainContent.ContentTemplate = instance.PAZContentTemplate;
                
                else
                
                    //instance.MainContent.ContentTemplate = instance.VisitTemplate;
                    switch (((MTreeViewVisit)e.NewValue).Type)
                    
                        // Generic Visits
                        case "APP":
                            instance.MainContent.ContentTemplate = instance.APPContentTemplate;
                            break;
                        default:
                            if (e.NewValue != null)
                            
#if LOG
                                Logger.Error("Should never be default");
#endif
                            
                            break;
                    
                

                instance.MainContent.DataContext = e.NewValue;

            
        
        public TreeViewContentControl()
        
            this.InitializeComponent();
        
    

【讨论】:

以上是关于UWP 从 TemplateSelector 更改为用户控件不再显示绑定的主要内容,如果未能解决你的问题,请参考以下文章

有没有办法在uwp应用程序中更改TextBox的行高?

如何以编程方式更改 Win 8.1 或 Win 10 UWP 应用的背景主题?

UWP - Flyout中的WinRT xaml工具包TreeView,在弹出窗口关闭后更改选择

更改背景时如何更改 UWP 按钮前景 PointerOver 属性?

如何更改 Xamarin UWP 应用名称

更改窗口大小 UWP 时更改控件大小布局