TreeView有时不更新

发布于 2024-12-22 03:23:26 字数 2663 浏览 0 评论 0原文

我有一个 TreeView 绑定到 ViewModel 中的 ObservableCollection。我有一个问题,如果我将一个项目添加到 ObservableCollection,有时它不会显示在 GUI 中。

我已经调试并发现该项目确实被添加,并且 CollectionChanged 事件确实在可观察集合上被触发。我的 GUI 的其他部分甚至会更新以反映新添加和选择的项目。唯一的问题是新项目没有显示在 TreeView 中。

这有点奇怪,因为有时它会出现,有时它会闪烁然后消失,有时它根本不显示。有什么想法吗?

编辑:

TreeView XAML:

 <TreeView Name="cedarTreeView"
              ItemsSource="{Binding CurrentFiles}" 
              ItemTemplate="{StaticResource MyFileTemplate}"
              VirtualizingStackPanel.IsVirtualizing="True"
              VirtualizingStackPanel.VirtualizationMode="Recycling">

        <TreeView.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel />
            </ItemsPanelTemplate>
        </TreeView.ItemsPanel>

        <TreeView.ItemContainerStyle>
            <Style TargetType="TreeViewItem">
                <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />                    
                <Setter Property="FontSize" Value="12" />
                <Setter Property="AllowDrop" Value="True" />
            </Style>
        </TreeView.ItemContainerStyle>
    </TreeView>

DataTemplate:

<HierarchicalDataTemplate x:Key="MyFileTemplate"
                              ItemTemplate="{StaticResource QualifierTemplate}"
                              ItemsSource="{Binding Qualifiers, Converter={StaticResource SortByNameConverter}}">
        <HierarchicalDataTemplate.ItemContainerStyle>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                <Setter Property="Visibility" Value="{Binding IsVisible, Converter={StaticResource BoolToVisConverter}}" />
            </Style>
        </HierarchicalDataTemplate.ItemContainerStyle>

        <TextBlock Text="{Binding Name, Mode=OneWay}" ToolTip="{Binding Name, Mode=OneWay}" Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType=TreeView}}">
           <TextBlock.ContextMenu>
                Some Context Menu Stuff Here
            </TextBlock.ContextMenu>
        </TextBlock>
    </HierarchicalDataTemplate>

我正在添加到 Qualifiers ObservableCollection。当我第一次打开文件并添加所有现有文件时,它会很好地显示它们。问题是当我尝试创建一个新的时。

I have a TreeView that is bound to an ObservableCollection in my ViewModel. I have an issue where if I add an item to the ObservableCollection, sometimes it isn't displayed in the GUI.

I have debugged and found that the item indeed gets added and the CollectionChanged event does indeed get fired on the observable collection. Other parts of my GUI even update to reflect the newly added and selected item. The only problem is that the new item does not show up in the TreeView.

It's a bit strange because sometimes it will show up, sometimes it will flicker up then go away, and sometimes it won't show up at all. Any ideas?

EDIT:

TreeView XAML:

 <TreeView Name="cedarTreeView"
              ItemsSource="{Binding CurrentFiles}" 
              ItemTemplate="{StaticResource MyFileTemplate}"
              VirtualizingStackPanel.IsVirtualizing="True"
              VirtualizingStackPanel.VirtualizationMode="Recycling">

        <TreeView.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel />
            </ItemsPanelTemplate>
        </TreeView.ItemsPanel>

        <TreeView.ItemContainerStyle>
            <Style TargetType="TreeViewItem">
                <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />                    
                <Setter Property="FontSize" Value="12" />
                <Setter Property="AllowDrop" Value="True" />
            </Style>
        </TreeView.ItemContainerStyle>
    </TreeView>

The DataTemplate:

<HierarchicalDataTemplate x:Key="MyFileTemplate"
                              ItemTemplate="{StaticResource QualifierTemplate}"
                              ItemsSource="{Binding Qualifiers, Converter={StaticResource SortByNameConverter}}">
        <HierarchicalDataTemplate.ItemContainerStyle>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                <Setter Property="Visibility" Value="{Binding IsVisible, Converter={StaticResource BoolToVisConverter}}" />
            </Style>
        </HierarchicalDataTemplate.ItemContainerStyle>

        <TextBlock Text="{Binding Name, Mode=OneWay}" ToolTip="{Binding Name, Mode=OneWay}" Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType=TreeView}}">
           <TextBlock.ContextMenu>
                Some Context Menu Stuff Here
            </TextBlock.ContextMenu>
        </TextBlock>
    </HierarchicalDataTemplate>

I am adding to the Qualifiers ObservableCollection. It displays them fine when I first open the file and add all of the existing ones. The problem is when i try to create a new one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无需解释 2024-12-29 03:23:26

您是否可能从单独的线程或任务修改集合?

Are you perhaps modifying the collection from a separate thread or task?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文