English 中文(简体)
数据化WPF 混合型号和分类
原标题:Databind WPF TreeView With Mixed Types and Categorized Sub-TreeViewItems

我试图在拥有几类森林的森林中打树。

基本结构就是这样:

Root  
 |_
 | Cat A
 |_
 | Cat B
 |_
   Cat C

每个类别必须单独进行可观察的收集。 我的大多数类别非常简单。 属于这一类的树木配上了书目,没有等级。 然而,我确实有一个类别,需要作为特别等级的代表。

以上树木中的“Cat C”形象也一样:

Servers
  |_
  | [SERVER A s DISPLAY NAME]
  |  |_
  |  | Namespaces
  |  |  |_
  |  |_   [NAMESPACE alpha s DISPLAY NAME]
  |  | Deployments
  |  |  |_
  |  |_   [DEPLOYMENT 1 s DISPLAY NAME]
  |_   Configuration File
    [SERVER B s DISPLAY NAME]

基本上,我想的是硬的代号 parent子树,用固定头盔。 然后,该项目应收集显示其名称的项目。 对于原父母的每个子女,应当有三个固定项目,有动态的儿童清单。

撰写这一文章,我认为这应该是一个非常简单的问题,可以解决。 然而,在经过几天后,我无法享有工作等级。 下面是我最难得的。 I ve利用“儿童”的综合收集,把姓名空间、部署和编造档案收集到一起。 然而,我不能将他们分开。

<TreeViewItem ItemsSource="{Binding Path=Configuration.Servers}" 
                      IsExpanded="True" >
            <TreeViewItem.ItemContainerStyle>
                <Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource {x:Type TreeViewItem}}">
                    <Setter Property="IsExpanded" Value="True"/>
                </Style>
            </TreeViewItem.ItemContainerStyle>

            <TreeViewItem.HeaderTemplate>
                <DataTemplate>
                    <Border Margin="0,2,2,0">
                        <StackPanel Orientation="Horizontal">
                            <Image Source="/WPF;component/Images/server_chart.png"
                                   Margin="0,0,5,0"/>
                            <TextBlock Text="Cognos Servers" />
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </TreeViewItem.HeaderTemplate>

            <TreeViewItem.Resources>
                <HierarchicalDataTemplate ItemSource="{Binding Path=Children}" DataType="{x:Type local:Server}">
                    <HierarchicalDataTemplate.ItemContainerStyle>
                        <Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource {x:Type TreeViewItem}}" >
                            <Setter Property="IsExpanded" Value="True"/>
                        </Style>
                    </HierarchicalDataTemplate.ItemContainerStyle>
                    <TextBlock Text="{Binding DisplayName}" PreviewMouseRightButtonDown="OnPreviewMouseRightButtonDown">
                        <TextBlock.ContextMenu>
                            <ContextMenu>
                                <MenuItem Header="Refresh" Click="TreeItemMenu_AddNewClient">
                                    <MenuItem.Icon>
                                        <Image Source="/WPF;component/Images/arrow_refresh.png" />
                                    </MenuItem.Icon>
                                </MenuItem>
                                <MenuItem Header="Add Client..." Click="TreeItemMenu_AddNewClient" />
                                <Separator />
                                <MenuItem Header="Remove" Click="TreeItemMenu_RemoveClick">
                                    <MenuItem.Icon>
                                        <Image Source="/WPF;component/Images/server_delete.png" />
                                    </MenuItem.Icon>
                                </MenuItem>
                            </ContextMenu>
                        </TextBlock.ContextMenu>
                    </TextBlock>
                </HierarchicalDataTemplate>

                <HierarchicalDataTemplate ItemsSource="{Binding Clients}" DataType="{x:Type local:Namespace}">
                    <HierarchicalDataTemplate.ItemContainerStyle>
                        <Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource {x:Type TreeViewItem}}" >
                            <Setter Property="IsExpanded" Value="True"/>
                        </Style>
                    </HierarchicalDataTemplate.ItemContainerStyle>
                    <TextBlock Text="{Binding DisplayName}" PreviewMouseRightButtonDown="OnPreviewMouseRightButtonDown" />
                </HierarchicalDataTemplate>

                <DataTemplate DataType="{x:Type local:Client}">
                    <TextBlock Text="{Binding DisplayName}"
                               ContextMenu="{StaticResource ResourceKey=ContextMenuTreeItem}" 
                               PreviewMouseRightButtonDown="OnPreviewMouseRightButtonDown" />
                </DataTemplate>
            </TreeViewItem.Resources>
最佳回答

Mike,

Mixing static and model data probably won t get you where you want. You ll have to create an actual ViewModel that contains the static nodes, etc. I highly recommend Josh Smith s TreeView ViewModel tutorials. This is the first one, but look around for more info. He is one of the most knowledgeable developer on that subject. We had great success following his recommendations.

问题回答

暂无回答




相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签