UWP从TemplateSelector切换到用户控件,不再显示绑定。

问题描述 投票:0回答:1

在我的问题中,按照Richard Zhang - MSFT提供的工作方案,我的问题是 当我调整窗口大小时,UWP Treeview与数据选择器在App.g.cs中产生错误。 我试图将第二组数据模板转换成一个控件,以便有一个更清晰的代码,并尝试解决其他一些可视化问题.代码编译,但它不显示任何东西。

使用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中不显示任何内容。页面被正确地改变了,静态文本被显示了,但没有真实的数据。

我想我做的和其他控件完全一样,但不明白我哪里弄错了。

c# xaml data-binding uwp user-controls
1个回答
0
投票

谢谢 Peter Spada 和他的惊人帮助,我能够让它工作。

问题是:我将instance.MainContent.ContentTemplate设置为一个DataTemplate,并将其与 x:DataType=”model:MTreeViewPaz”. 然而, instance.MainContent 从来没有 ContentDataContext 设置,所以它使用的是默认的 DataContext. 默认的 DataContext 继承自XAML树,并且来自于根的 ShellPage:

这就是为什么 InvalidCastException 落空 ShellPageMTreeViewPaz.

我可以通过手动设置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();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.