UWP TreeView-绑定后面的数据更新使“可扩展”图标消失 [英] UWP TreeView - Update of data behind a binding makes the Expandable icon to disappear

查看:91
本文介绍了UWP TreeView-绑定后面的数据更新使“可扩展”图标消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用TreeView,我有一个Patient结构:

With a TreeView I have a Patient structure:

        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>

和CustomControl:

and CustomControl:

<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="&#xE77B;" />
                <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选择患者的排序顺序。
他们每个人都调用相同的过程:

I have then 4 AppBarTogleButton to select the sorting order of the Patients. Each of them invoke the same procedure:

        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中:

and in the 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;
        }

我用新分类的病人更新了病人。
的问题是,我几次更改了排序顺序后,>扩展了叶子分配器:

I update the Patients with the new sorted one. The issue is that after I change the sorting order couple of time the > to expand the leafs disapper:

我检查了一下,数据结构是完全相同的(除了顺序)并纠正

I've checked and the data structure is absolutely identical (apart from the order) and correct

这怎么可能?

---其他信息---

--- Additional info ---

您可以看到子结构仍然存在

As you can see the child structure is still there

推荐答案


UWP TreeView-绑定后面的数据更新使Expandable图标消失

UWP TreeView - Update of data behind a binding makes the Expandable icon to disappear

请检查此行< winui:TreeViewItem IsExpanded = False ItemsSource = {x:Bind Visits}> ,您使用的是 x:绑定无法响应访问属性的OneTime模型已更改,请使用 OneWay 对其进行编辑TwoWay 模型并请确保您已为 Visits 属性调用了OnPropertyChanged事件。

Please check this line <winui:TreeViewItem IsExpanded="False" ItemsSource="{x:Bind Visits}">, you used x:Bind OneTime model that will not response Visits property changed, please edit it with OneWay or TwoWay model and please sure you have call OnPropertyChanged event for Visits property.

< winui:TreeViewItem IsExpanded = False ItemsSource = {x:Bind Visits,Mode = OneWay}>

这篇关于UWP TreeView-绑定后面的数据更新使“可扩展”图标消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆