Следуя рабочему решению, предоставленному Ричардом Чжаном - MSFT в моем вопросе UWP Treeview с селектором данных, генерирующим ошибку в App.g.cs, когда я изменяю размер окна Я попытался преобразовать вторую группу данных шаблоны в элемент управления, чтобы иметь более понятный код и попытаться решить некоторые другие проблемы визуализации. Код компилируется, но ничего не показывает.
Рабочий код с помощью TreeView Template Selector:
<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. Страница изменена правильно, отображается stati c текст, но не реальные данные.
Я думаю, что сделал то же самое, что и другой элемент управления, но не могу понять, что я напутал