【发布时间】:2020-05-28 13:32:38
【问题描述】:
使用绑定到三层分层集合(订单 - SKU - 位置)的树视图时。我正在使用 DataTemplateSelector 来选择要使用的 TreeViewItem 模板。当我滚动时,我得到一个与post 完全相同的无效异常 - 如果我滚动得更快,异常似乎发生得更快。
我在该帖子中尝试了两种建议的解决方案(将树视图包装在滚动查看器中,以及自定义用户控件)。包装 TreeView 没有帮助,我无法让自定义用户控件解决方案为我正常工作,我想知道这是否与我有一个三层集合而不是两层的事实有关。自定义 UserControl 的代码与链接帖子中的代码完全相同,只是针对我的收藏进行了修改。当我尝试它时,它只会将集合的一层绑定到树视图。我正在寻找的是一种解决方案,可以防止框架代码重用模板,这似乎是导致异常的原因。
这是我的 XAML 和当前产生异常的 TreeView 的代码:
<Page
x:Class="PickSheetManager.Views.PickSheetManagerPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:winui="using:Microsoft.UI.Xaml.Controls"
xmlns:model="using:PickSheetManager.Core.Models"
xmlns:behaviors="using:PickSheetManager.Behaviors"
xmlns:templateSelectors="using:PickSheetManager.TemplateSelectors"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:controls1="using:PickSheetManager.Controls"
xmlns:ic="using:Microsoft.Xaml.Interactions.Core"
xmlns:i="using:Microsoft.Xaml.Interactivity"
Style="{StaticResource PageStyle}"
behaviors:NavigationViewHeaderBehavior.HeaderMode="Never"
mc:Ignorable="d">
<Page.Resources>
<DataTemplate x:Key="OrderTemplate" x:DataType="model:Order">
<winui:TreeViewItem
AutomationProperties.Name="{x:Bind PoIDx}"
ItemsSource="{x:Bind SKUs}" IsExpanded="True">
<StackPanel x:Name="stack" Orientation="Horizontal">
<i:Interaction.Behaviors>
<ic:DataTriggerBehavior Binding ="{x:Bind NotEnoughInventory}" Value="True">
<ic:ChangePropertyAction TargetObject="{Binding ElementName=stack}" PropertyName="Background" Value="Red" />
</ic:DataTriggerBehavior>
</i:Interaction.Behaviors>
<CheckBox IsChecked="{x:Bind IsSelected, Mode=TwoWay}" Padding="5" MinWidth="0"/>
<TextBlock Text="PoIDx: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind PoIDx}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Delivery: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind DeliveryType}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Delivery: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind DeliveryType}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Type: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind OrderType}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Total Quantity: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind TotalQuantity}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Printed: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind IsPrinted}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Notes: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBox Text="{x:Bind Notes, Mode=TwoWay}" Margin="{StaticResource XXSmallTopRightBottomMargin}" Width="150"/>
</StackPanel>
</winui:TreeViewItem>
</DataTemplate>
<DataTemplate x:Key="SKUTemplate" x:DataType="model:SKU">
<winui:TreeViewItem
AutomationProperties.Name="{x:Bind SKUNum}"
ItemsSource="{x:Bind Locations}" IsExpanded="True">
<StackPanel x:Name="stack2" Orientation="Horizontal">
<i:Interaction.Behaviors>
<ic:DataTriggerBehavior Binding ="{x:Bind NotEnoughInventory}" Value="True">
<ic:ChangePropertyAction TargetObject="{Binding ElementName=stack2}" PropertyName="Background" Value="Yellow" />
</ic:DataTriggerBehavior>
</i:Interaction.Behaviors>
<CheckBox IsChecked="{x:Bind IsSelected, Mode=TwoWay}" Padding="5" MinWidth="0" />
<TextBlock Text="SKU: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind SKUNum}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Needed: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind NeedQuantity}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Available: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind AvailableQuantity}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
</StackPanel>
</winui:TreeViewItem>
</DataTemplate>
<DataTemplate x:Key="LocationTemplate" x:DataType="model:Location">
<winui:TreeViewItem
AutomationProperties.Name="{x:Bind LocationName}"
IsExpanded="True">
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{x:Bind IsSelected, Mode=TwoWay}" Padding="5" MinWidth="0" />
<TextBlock Text="{x:Bind LocationName}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Pick Type: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind PickType}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Pick Quantity: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind PickQuantity}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Available Quantity: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind AvailableQuantity}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBox Text="{x:Bind Notes, Mode=TwoWay}" Margin="{StaticResource XXSmallTopRightBottomMargin}" Width="150" />
</StackPanel>
</winui:TreeViewItem>
</DataTemplate>
<templateSelectors:SampleDataTemplateSelector x:Key="TreeViewTemplateSelector"
OrderDetailTemplate="{StaticResource OrderTemplate}"
SKUDetailTemplate="{StaticResource SKUTemplate}"
LocationDetailTemplate="{StaticResource LocationTemplate}" />
</Page.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="treeViewColumn" Width="6*" />
<ColumnDefinition x:Name="detailViewColumn" Width="4*" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition x:Name="treeViewRow" Height="8*" />
<RowDefinition x:Name="controlRow" Height="2*" />
</Grid.RowDefinitions>
<ScrollViewer x:Name="WrapViewer" Grid.Row="0" Grid.Column="0" Padding="{StaticResource DetailPageMargin}">
</ScrollViewer>
<winui:TreeView
x:Name="treeView"
Grid.Row="0"
SelectionMode="Single"
ItemsSource="{x:Bind ViewModel.openOrders}"
ItemTemplateSelector="{StaticResource TreeViewTemplateSelector}">
<i:Interaction.Behaviors>
<behaviors:TreeViewCollapseBehavior x:Name="collapseBehavior" />
<ic:EventTriggerBehavior EventName="ItemInvoked">
<ic:InvokeCommandAction Command="{x:Bind ViewModel.ItemInvokedCommand}" />
</ic:EventTriggerBehavior>
</i:Interaction.Behaviors>
</winui:TreeView>
</Grid>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition x:Name="detailViewRow" Height="4*" />
<RowDefinition x:Name="iarRow" Height="4*" />
<RowDefinition x:Name="controlRow2" Height="2*" />
</Grid.RowDefinitions>
</Grid>
</Grid>
被绑定的集合
public ObservableCollection<Order> openOrders { get; } = new ObservableCollection<Order>();
public async Task LoadDataAsync()
{
var data2 = await OrderDataLoader.GetTreeViewDataAsync();
foreach (var item in data2)
{
openOrders.Add(item);
}
}
以及 DataTemplateSelector 代码:
public class SampleDataTemplateSelector : DataTemplateSelector
{
public DataTemplate OrderDetailTemplate { get; set; }
public DataTemplate SKUDetailTemplate { get; set; }
public DataTemplate LocationDetailTemplate { get; set; }
protected override DataTemplate SelectTemplateCore(object item)
{
return GetTemplate(item) ?? base.SelectTemplateCore(item);
}
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
return GetTemplate(item) ?? base.SelectTemplateCore(item, container);
}
private DataTemplate GetTemplate(object item)
{
switch (item)
{
case Order order:
return OrderDetailTemplate;
case SKU sku:
return SKUDetailTemplate;
case Location location:
return LocationDetailTemplate;
}
return null;
}
}
更新
我实际上已经尝试了两种不同的尝试来使用户控件正常工作。两者都只显示了一层。
尝试 #1 用户控件 XAML:
<UserControl
x:Class="PickSheetManager.Controls.OrderTreeItemControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:winui="using:Microsoft.UI.Xaml.Controls"
xmlns:model="using:PickSheetManager.Core.Models"
xmlns:behaviors="using:PickSheetManager.Behaviors"
xmlns:templateSelectors="using:PickSheetManager.TemplateSelectors"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:controls1="using:PickSheetManager.Controls"
xmlns:ic="using:Microsoft.Xaml.Interactions.Core"
xmlns:i="using:Microsoft.Xaml.Interactivity"
mc:Ignorable="d">
<UserControl.Resources>
<DataTemplate x:Name="OrderTemplate" x:DataType="model:Order">
<StackPanel x:Name="stack" Orientation="Horizontal">
<i:Interaction.Behaviors>
<ic:DataTriggerBehavior Binding ="{x:Bind NotEnoughInventory}" Value="True">
<ic:ChangePropertyAction TargetObject="{Binding ElementName=stack}" PropertyName="Background" Value="Red" />
</ic:DataTriggerBehavior>
</i:Interaction.Behaviors>
<CheckBox IsChecked="{x:Bind IsSelected, Mode=TwoWay}" Padding="5" MinWidth="0"/>
<TextBlock Text="PoIDx: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind PoIDx}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Delivery: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind DeliveryType}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Delivery: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind DeliveryType}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Type: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind OrderType}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Total Quantity: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind TotalQuantity}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Printed: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind IsPrinted}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Notes: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBox Text="{x:Bind Notes, Mode=TwoWay}" Margin="{StaticResource XXSmallTopRightBottomMargin}" Width="150"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Name="SKUTemplate" x:DataType="model:SKU">
<StackPanel x:Name="stack2" Orientation="Horizontal">
<i:Interaction.Behaviors>
<ic:DataTriggerBehavior Binding ="{x:Bind NotEnoughInventory}" Value="True">
<ic:ChangePropertyAction TargetObject="{Binding ElementName=stack2}" PropertyName="Background" Value="Yellow" />
</ic:DataTriggerBehavior>
</i:Interaction.Behaviors>
<CheckBox IsChecked="{x:Bind IsSelected, Mode=TwoWay}" Padding="5" MinWidth="0" />
<TextBlock Text="SKU: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind SKUNum}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Needed: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind NeedQuantity}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Available: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind AvailableQuantity}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Name="LocationTemplate" x:DataType="model:Location">
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{x:Bind IsSelected, Mode=TwoWay}" Padding="5" MinWidth="0" />
<TextBlock Text="{x:Bind LocationName}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Pick Type: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind PickType}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Pick Quantity: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind PickQuantity}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Available Quantity: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind AvailableQuantity}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBox Text="{x:Bind Notes, Mode=TwoWay}" Margin="{StaticResource XXSmallTopRightBottomMargin}" Width="150" />
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<Grid>
<ContentControl x:Name="MainContent"/>
</Grid>
</UserControl>
尝试 #1 .cs
namespace PickSheetManager.Controls
{
public sealed partial class OrderTreeItemControl : UserControl
{
public OrderTreeItemControl()
{
this.InitializeComponent();
}
public Order Data
{
get { return (OpenOrdersBase)GetValue(DataProperty); }
set { SetValue(DataProperty, value); }
}
public static readonly DependencyProperty DataProperty =
DependencyProperty.Register("Data", typeof(OpenOrdersBase), typeof(OrderTreeItemControl), new PropertyMetadata(null, new PropertyChangedCallback(Data_Changed)));
private static void Data_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue != null)
{
var instance = d as OrderTreeItemControl;
if (e.NewValue is Location)
{
instance.MainContent.ContentTemplate = instance.LocationTemplate;
}
else if (e.NewValue is SKU)
{
instance.MainContent.ContentTemplate = instance.SKUTemplate;
}
else
{
instance.MainContent.ContentTemplate = instance.OrderTemplate;
}
}
}
}
}
尝试 #1 模型类
public class OpenOrdersBase
{
public ObservableCollection<Order> Orders { get; } = new ObservableCollection<Order>();
}
public class Order : OpenOrdersBase
{
public bool IsSelected { get; set; }
public string OrderNumber { get; set; }
public string PoIDx { get; set; }
...
public ICollection<SKU> SKUs { get; set; }
}
public class SKU : OpenOrderBase
{
public bool IsSelected { get; set; }
public string SKUNum { get; set; }
...
public ICollection<Location> Locations { get; set; }
}
public class Location : OpenOrdersBase
{
public bool IsSelected { get; set; }
...
public string Notes { get; set; }
}
尝试 #1 页面 XAML
<DataTemplate x:Key="BaseTemplate" x:DataType="model:Order">
<winui:TreeViewItem
IsExpanded="False"
ItemsSource="{x:Bind SKUs}">
<controls1:TestControl Data="{Binding}"/>
</winui:TreeViewItem>
</DataTemplate>
...
<winui:TreeView
x:Name="treeView"
Grid.Row="0"
SelectionMode="Single"
Margin="10,10,10,10"
ItemsSource="{x:Bind ViewModel.openOrders}"
ItemTemplate="{StaticResource BaseTemplate}">
<i:Interaction.Behaviors>
<behaviors:TreeViewCollapseBehavior x:Name="collapseBehavior" />
<ic:EventTriggerBehavior EventName="ItemInvoked">
<ic:InvokeCommandAction Command="{x:Bind ViewModel.ItemInvokedCommand}" />
</ic:EventTriggerBehavior>
</i:Interaction.Behaviors>
</winui:TreeView>
尝试 #2 最大的区别在于 UserControl XAML 和模型中 用户控件 XAML
<UserControl
x:Class="PickSheetManager.Controls.OrderTreeItemControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:winui="using:Microsoft.UI.Xaml.Controls"
xmlns:model="using:PickSheetManager.Core.Models"
xmlns:behaviors="using:PickSheetManager.Behaviors"
xmlns:templateSelectors="using:PickSheetManager.TemplateSelectors"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:controls1="using:PickSheetManager.Controls"
xmlns:ic="using:Microsoft.Xaml.Interactions.Core"
xmlns:i="using:Microsoft.Xaml.Interactivity"
mc:Ignorable="d">
<UserControl.Resources>
<DataTemplate x:Name="OrderTemplate" x:DataType="model:Order">
<winui:TreeViewItem
AutomationProperties.Name="{x:Bind PoIDx}"
ItemsSource="{x:Bind SKUs}"
IsExpanded="True">
<StackPanel x:Name="stack" Orientation="Horizontal">
<i:Interaction.Behaviors>
<ic:DataTriggerBehavior Binding ="{x:Bind NotEnoughInventory}" Value="True">
<ic:ChangePropertyAction TargetObject="{Binding ElementName=stack}" PropertyName="Background" Value="Red" />
</ic:DataTriggerBehavior>
</i:Interaction.Behaviors>
<CheckBox IsChecked="{x:Bind IsSelected, Mode=TwoWay}" Padding="5" MinWidth="0"/>
<TextBlock Text="PoIDx: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind PoIDx}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Delivery: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind DeliveryType}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Delivery: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind DeliveryType}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Type: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind OrderType}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Total Quantity: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind TotalQuantity}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Printed: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind IsPrinted}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Notes: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBox Text="{x:Bind Notes, Mode=TwoWay}" Margin="{StaticResource XXSmallTopRightBottomMargin}" Width="150"/>
</StackPanel>
</winui:TreeViewItem>
</DataTemplate>
<DataTemplate x:Name="SKUTemplate" x:DataType="model:SKU">
<winui:TreeViewItem
AutomationProperties.Name="{x:Bind SKUNum}"
ItemsSource="{x:Bind Locations}"
IsExpanded="True">
<StackPanel x:Name="stack2" Orientation="Horizontal">
<i:Interaction.Behaviors>
<ic:DataTriggerBehavior Binding ="{x:Bind NotEnoughInventory}" Value="True">
<ic:ChangePropertyAction TargetObject="{Binding ElementName=stack2}" PropertyName="Background" Value="Yellow" />
</ic:DataTriggerBehavior>
</i:Interaction.Behaviors>
<CheckBox IsChecked="{x:Bind IsSelected, Mode=TwoWay}" Padding="5" MinWidth="0" />
<TextBlock Text="SKU: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind SKUNum}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Needed: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind NeedQuantity}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Available: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind AvailableQuantity}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
</StackPanel>
</winui:TreeViewItem>
</DataTemplate>
<DataTemplate x:Name="LocationTemplate" x:DataType="model:Location">
<winui:TreeViewItem
AutomationProperties.Name="{x:Bind LocationName}"
IsExpanded="True">
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{x:Bind IsSelected, Mode=TwoWay}" Padding="5" MinWidth="0" />
<TextBlock Text="{x:Bind LocationName}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Pick Type: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind PickType}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Pick Quantity: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind PickQuantity}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="Available Quantity: " Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBlock Text="{x:Bind AvailableQuantity}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
<TextBox Text="{x:Bind Notes, Mode=TwoWay}" Margin="{StaticResource XXSmallTopRightBottomMargin}" Width="150" />
</StackPanel>
</winui:TreeViewItem>
</DataTemplate>
</UserControl.Resources>
<Grid>
<ContentControl x:Name="MainContent"/>
</Grid>
</UserControl>
在用户控件的代码中,我只是将 OpenOrdersBase 更新为 Order,因为我从所有模型中删除了继承。
最终页面 XAML 以这种方式更改
<DataTemplate x:Key="BaseTemplate" x:DataType="model:Order">
<controls1:TestControl Data="{Binding}"/>
</DataTemplate>
【问题讨论】:
-
你好,对于更复杂的数据结构,使用
UserControl在控件内部进行判断是一种思路。你需要根据你的数据结构调整UserControl的写法,你说UserControl只显示一层,你能说明你是怎么写这个UserControl的吗? -
我明天一上班就会把代码贴出来。谢谢。