如何使用基于.Net类属性的值自动更新WPF TreeViewItems? [英] How to get updated automatically WPF TreeViewItems with values based on .Net class properties?

查看:69
本文介绍了如何使用基于.Net类属性的值自动更新WPF TreeViewItems?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好. 我有一个带有从InotifyPropertyChange派生的数据的类.数据来自后台线程,该后台线程在某些位置搜索具有特定扩展名的文件.该类的公共属性通过更新单独线程中的数据来响应事件OnPropertyChange.此外,在XAML TreeView中基于HierarhicalDataTemplates进行了描述.模板中的每个TextBlock都提供了ItemsSource ="{Binding FoundFilePaths,Mode = OneWay,NotifyOnTargetUpdated = True}".

Good morning. I have a class with data derived from InotifyPropertyChange. The data come from a background thread, which searches for files with certain extension in certain locations. Public property of the class reacts to an event OnPropertyChange by updating data in a separate thread. Besides, there are described in XAML TreeView, based on HierarhicalDataTemplates. Each TextBlock inside templates supplied ItemsSource = "{Binding FoundFilePaths, Mode = OneWay, NotifyOnTargetUpdated = True}".

 <TreeView  x:Name="FoundFiles_TreeView"  Opacity="15" Background="White"   BorderThickness="5" FontFamily="Arial" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5" VerticalAlignment="Top" Height="360" FontWeight="Bold" Foreground="#FF539DBE" TargetUpdated="FoundFiles_TreeView_TargetUpdated">
            <TreeView.ItemContainerStyle >
                <Style TargetType="{x:Type TreeViewItem}">
                    <Setter Property="TreeViewItem.Tag" Value="InfoNode" />
                    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                    <Setter Property="Foreground" Value="Brown"/>
                    <Style.Triggers>
                        <Trigger Property="IsMouseCaptured" Value="True">
                            <Setter Property="IsSelected" Value="True"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TreeView.ItemContainerStyle>
            <TreeView.Resources>
                <HierarchicalDataTemplate  DataType = "{x:Type lightvedo:FilesInfoStore}"  ItemsSource="{Binding FoundFilePaths, Mode=OneWay, NotifyOnTargetUpdated=True}">
                    <!--Здесь указываются узлы дерева-->
                    <StackPanel x:Name ="TreeNodeStackPanel" Orientation="Horizontal">
                        <TextBlock Margin="5,5,5,5" TargetUpdated="TextBlockExtensions_TargetUpdated">
         <TextBlock.Text>
          <MultiBinding StringFormat="Files with Extension  {0}">
           <Binding Path="FileExtension"/>
          </MultiBinding>
         </TextBlock.Text>
                        </TextBlock>
                        <Button x:Name="OpenFolderForThisFiles" Click="OnOpenFolderForThisFiles_Click" Margin="5, 3, 5, 3" Width="22" Background="Transparent" BorderBrush="Transparent" BorderThickness="0.5">
                            <Image Source="images\Folder.png" Height="16" Width="20" >
                            </Image>
                        </Button>
                    </StackPanel>
                </HierarchicalDataTemplate>
                <HierarchicalDataTemplate DataType="{x:Type lightvedo:FilePathsStore}">
                    <TextBlock Text="{Binding FilePaths, Mode=OneWay, NotifyOnTargetUpdated=True}" TargetUpdated="OnTreeViewNodeChildren_Update" />
                </HierarchicalDataTemplate>
            </TreeView.Resources>
            <TreeView.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform AngleX="-0.083"/>
                    <RotateTransform/>
                    <TranslateTransform X="-0.249"/>
                </TransformGroup>
            </TreeView.RenderTransform>
            <TreeView.BorderBrush>
                <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                    <GradientStop Color="#FF74591F" Offset="0" />
                    <GradientStop Color="#FF9F7721" Offset="1" />
                    <GradientStop Color="#FFD9B972" Offset="0.49" />
                </LinearGradientBrush>
            </TreeView.BorderBrush>
        </TreeView>

问:为什么来自INotifyPropertyChange的类中的数据不影响树项目的显示.我是否了解:该界面会使INotifyPropertyChange自动重绘TreeViewItems还是我需要手动执行此操作?当前TreeViewItems尚未更新,PropertyChamged始终为null.感觉没有事件OnPropertyChanged的订阅者.

Q: Why is the data from a class derived from INotifyPropertyChange does not affect the display of tree items. Do I understand: The interface will make INotifyPropertyChange be automatically redrawn TreeViewItems or do I need to manually carry out this operation? Currently TreeViewItems not updated and PropertyChamged always null. A feeling that no subscribers to the event OnPropertyChanged.

推荐答案

您无需设置NotifyOnTargetUpdated.

相反,请确保提高 PropertyChanged 事件(通过

Instead, make sure to raise the PropertyChanged event (with the appropriate property-name passed with the PropertyChangedEventArgs passed to the handler) on the parent entity each time the paths collection is updated, or have the navigation property be an implementation of INotifyCollectionChanged.

这篇关于如何使用基于.Net类属性的值自动更新WPF TreeViewItems?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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