c# - UWP TreeView - 更新绑定后的数据使可扩展图标消失
问题描述
使用 TreeView 我有一个患者结构:
private ObservableCollection<MTreeViewPaz> _Patients;
public ObservableCollection<MTreeViewPaz> Patients
{
get { return _Patients; }
private set { Set(ref _Patients, value); }
}
和绑定到它的树视图:
<winui:TreeView
x:Name="treeView"
Grid.Row="1"
Expanding="treeView_Expanding"
ItemInvoked="OnItemInvoked"
ItemTemplate="{StaticResource ItemTemplate}"
ItemsSource="{x:Bind Patients, Mode=OneWay}"
SelectionMode="Single" />
带有项目模板:
<DataTemplate x:Key="ItemTemplate" x:DataType="model:MTreeViewBase">
<winui:TreeViewItem IsExpanded="False" ItemsSource="{x:Bind Visits}">
<controls1:TreeViewControl Data="{x:Bind}" />
</winui:TreeViewItem>
</DataTemplate>
和自定义控件:
<UserControl
x:Class="TitoDoc2020.Views.TreeViewControl"
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:local="using:TitoDoc2020.Views"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:model="using:TitoDoc2020.Models"
d:DesignHeight="300"
d:DesignWidth="400"
mc:Ignorable="d">
<UserControl.Resources>
<DataTemplate x:Name="PAZTemplate" x:DataType="model:MTreeViewPaz">
<StackPanel Orientation="Horizontal">
<FontIcon
Margin="{StaticResource XXSmallTopRightBottomMargin}"
FontFamily="{StaticResource SymbolThemeFontFamily}"
Glyph="" />
<TextBlock
Margin="{StaticResource XXSmallTopRightBottomMargin}"
VerticalAlignment="Center"
Text="{x:Bind Name}" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Name="VisitTemplate" x:DataType="model:MTreeViewVisit">
<StackPanel Orientation="Horizontal">
<FontIcon
Margin="{StaticResource XXSmallTopRightBottomMargin}"
FontFamily="{StaticResource SymbolThemeFontFamily}"
Foreground="{x:Bind ImageColor}"
Glyph="{x:Bind ImageSrc}" />
<TextBlock
Margin="{StaticResource XXSmallTopRightBottomMargin}"
VerticalAlignment="Center"
Text="{x:Bind VisitDescr}" />
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<Grid>
<ContentControl x:Name="MainContent" />
</Grid>
</UserControl>
然后我有 4 个 AppBarTogleButton 来选择患者的排序顺序。他们每个人都调用相同的过程:
private async void AppBarToggleButton_Checked(object sender, RoutedEventArgs e)
{
string sorting = await ApplicationData.Current.LocalSettings.ReadAsync<string>("TreeViewSort");
switch (((Windows.UI.Xaml.Controls.AppBarToggleButton)sender).Name)
{
case "DescDate":
if (sorting == "DescDate")
{
return;
}
AsceDate.IsChecked = false;
AsceAlph.IsChecked = false;
DescAlph.IsChecked = false;
break;
case "AsceDate":
if (sorting == "AsceDate")
{
return;
}
DescDate.IsChecked = false;
AsceAlph.IsChecked = false;
DescAlph.IsChecked = false;
break;
case "DescAlph":
if (sorting == "DescAlph")
{
return;
}
AsceDate.IsChecked = false;
DescDate.IsChecked = false;
AsceAlph.IsChecked = false;
break;
case "AsceAlph":
if (sorting == "AsceAlph")
{
return;
}
AsceDate.IsChecked = false;
DescDate.IsChecked = false;
DescAlph.IsChecked = false;
break;
default:
break;
}
await ApplicationData.Current.LocalSettings.SaveAsync("TreeViewSort", ((Windows.UI.Xaml.Controls.AppBarToggleButton)sender).Name);
_sorting = ((Windows.UI.Xaml.Controls.AppBarToggleButton)sender).Name;
await SortTreeAsync(false);
}
在 SortTreeAsync 中:
private async Task SortTreeAsync(bool setCheck)
{
ObservableCollection<MTreeViewPaz> _patients;
if (treeView.Visibility == Visibility.Collapsed)
{
return;
}
treeView.Visibility = Visibility.Collapsed;
switch (_sorting)
{
case "DescDate":
if (setCheck) DescDate.IsChecked = true;
Patients = new ObservableCollection<MTreeViewPaz>(
from i in Patients orderby i.Data descending, i.Cognome, i.Nome select i);
/* await treeView.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
Patients = _patients;
});
*/
break;
case "AsceDate":
if (setCheck) AsceDate.IsChecked = true;
Patients = new ObservableCollection<MTreeViewPaz>(
from i in Patients orderby i.Data, i.Cognome, i.Nome select i);
break;
case "DescAlph":
if (setCheck) DescAlph.IsChecked = true;
Patients = new ObservableCollection<MTreeViewPaz>(
from i in Patients orderby i.Cognome descending, i.Nome descending, i.Data descending select i);
break;
case "AsceAlph":
if (setCheck) AsceAlph.IsChecked = true;
Patients = new ObservableCollection<MTreeViewPaz>(
from i in Patients orderby i.Cognome, i.Nome, i.Data descending select i);
break;
default:
DescDate.IsChecked = true;
break;
}
treeView.Visibility = Visibility.Visible;
return;
}
我用新排序的患者更新患者。问题是,在我更改排序顺序几次后 > 展开叶子消失:
我已经检查过了,数据结构是完全一样的(除了顺序)并且是正确的
这怎么可能?
- - 附加信息 - -
如您所见,子结构仍然存在
解决方案
UWP TreeView - 更新绑定后的数据使可扩展图标消失
请检查此行 <winui:TreeViewItem IsExpanded="False" ItemsSource="{x:Bind Visits}">
,您使用x:Bind
了不会响应Visits
属性更改的 OneTime 模型,请使用OneWay或TwoWay模型对其进行编辑,并确保您已为属性调用 OnPropertyChanged 事件Visits
。
<winui:TreeViewItem IsExpanded="False" ItemsSource="{x:Bind Visits, Mode=OneWay}">
推荐阅读
- azure - 带有 Msal for Angular 6 的多租户 Azure Active Directory 应用程序
- svn - 从 BDB SVN 存储库中恢复源代码
- c - 如何在 C 中使用 fork() 制作倾斜树?
- python - 如何使用 python 3 的 anaconda 安装 rpy2 以在同一个 jupyter 笔记本中运行 R 和 Python?
- c++ - 为什么我的石头剪刀布游戏无法运行?
- html - 如何更改特定日期或季节的图像
- sql - 标题表中的计算字段,用于汇总来自不同表的值
- docker - 查找最新版本 docker 镜像的实际版本
- node.js - iothub 错误代码:无效协议版本
- nuget - Nuget 不尊重包参考配置?