WPF层次树视图:自我分层和平面源的组合绑定

时间:2015-10-03 14:32:31

标签: wpf entity-framework treeview hierarchical-data hierarchicaldatatemplate

我的名字是Dominique,来自瑞士,我是一名非专业开发人员 - 所以请耐心等待;)

自从3周以来,我一直在努力完成这些简单的任务。 花费时间“谷歌搜索”和阅读。好吧,一方面它很棒,因为作为一个非专业人士,我学到了很多关于MVVM / Binding / Entityframework等等。而且我总是那么努力 - 我使用VB.NET - 并且必须转换大部分代码从C Sharp发现到VB.NET;) 但另一方面,这令人沮丧,我绝望地需要你专业人士的暗示。那么,我的Toppic是什么:

完成标题:

WPF分层树视图:使用EntityFramework 6和数据库优先方法对自我分层源和平面源进行组合绑定和模板化。

的DataModel:

图片:http://i.stack.imgur.com/LIb1v.png

预期的树视图

我有两种类型的物品:

  • “维度”来自自我提供的源(XELL_DIMENSION)和 分层项目。未知/打开Leveldepth。
  • “元素”来自平面源(XELL_ELEMENTS)a

图片:http://i.stack.imgur.com/QeFwe.png

好的,这是我到目前为止所取得的成就:

主窗口类:

Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
        Dim elementsContext As New XELLEntities()
        Tree.DataContext = elementsContext.XELL_DIMENSION.Include("XELL_ELEMS").ToList()
        Tree.ItemsSource = elementsContext.XELL_DIMENSION.Where(Function(y) y.DIMEN_PARENT_ID Is Nothing).ToList()
End Sub        

XAML代码:

 <TreeView Name="Tree" HorizontalAlignment="Left" Height="187" Margin="10,10,0,0" VerticalAlignment="Top" Width="415">
            <TreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type local2:XELL_DIMENSION}" ItemsSource="{Binding DIM_ALL_NODE}">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding DIMEN_ID}"/>
                        <StackPanel Orientation="Vertical">
                            <TextBlock Text="{Binding DIMEN_BEZ_LONG}"/>
                            <ListBox Name="Listy" ItemsSource="{Binding XELL_ELEMS}" DisplayMemberPath="ELEM_BEZ_LONG" BorderBrush="Transparent" BorderThickness="0"/>
                        </StackPanel>                            
                    </StackPanel>
                </HierarchicalDataTemplate>
            </TreeView.Resources>                           
        </TreeView>

结果:

hxxp://i.stack.imgur.com/Bghfs.png

(对不起,我没有嵌入图片的声望点,包括两个以上的链接,值得考虑的规则)

问题:

所以这是我最接近的,我使用了Listbox作为元素 - 但是这个 对我没有解决方案。

- 有关如何解决问题的建议吗?

我非常感谢提供的任何提示或CodeSnipped。

原谅我的非母语英语 - 多米尼克

1 个答案:

答案 0 :(得分:1)

尤里卡! -SOLVED

我不得不使用Converter和TemplateSelector。

<强> XAML:

<TreeView Name="Tree" HorizontalAlignment="Left" Height="187" Margin="10,10,0,0" VerticalAlignment="Top" Width="415"> <TreeView.Resources>
            <local3:LeafDataTemplateSelector x:Key="LeafDataTemplateSelector" />
            <HierarchicalDataTemplate  DataType="{x:Type local2:XELL_DIMENSION}" ItemTemplateSelector="{StaticResource LeafDataTemplateSelector}">
                <HierarchicalDataTemplate.ItemsSource>
                    <MultiBinding Converter="{StaticResource SimpleFolderConverter}">
                        <Binding Path="DIM_ALL_NODE" />
                        <Binding Path="XELL_ELEMS" />
                    </MultiBinding>
                </HierarchicalDataTemplate.ItemsSource>
                <StackPanel Orientation="Horizontal">
                    <Image HorizontalAlignment="Center" Height="20" VerticalAlignment="Center" Width="20" Source="MVVM/VIEW/IMAGES/Nodes.png" Stretch="Uniform" />
                    <TextBlock Foreground="#FF3399FF" Text="{Binding DIMEN_BEZ_LONG}" FontWeight="Bold"/>
                </StackPanel>
            </HierarchicalDataTemplate> <HierarchicalDataTemplate x:Key="Dimension" DataType="{x:Type local2:XELL_DIMENSION}" ItemTemplateSelector="{StaticResource LeafDataTemplateSelector}">
                <HierarchicalDataTemplate.ItemsSource>
                    <MultiBinding Converter="{StaticResource SimpleFolderConverter}">
                       <Binding Path="XELL_ELEMS" />
                        <Binding Path="DIM_ALL_NODE" />
                    </MultiBinding>
                </HierarchicalDataTemplate.ItemsSource>
                <StackPanel Height="25" Orientation="Horizontal" ToolTip="Installation File">
                    <Image HorizontalAlignment="Center" Height="20" VerticalAlignment="Center" Width="20" Source="MVVM/VIEW/IMAGES/Nodes.png" Stretch="Uniform" />
                    <TextBlock Foreground="#FF3399FF" Text="{Binding DIMEN_BEZ_LONG}" FontWeight="Bold"/>
                </StackPanel>
            </HierarchicalDataTemplate> <DataTemplate x:Key="Element" DataType="{x:Type local2:XELL_ELEMENT}">
                <StackPanel Height="25" Orientation="Horizontal" ToolTip="Installation File">
                    <Image HorizontalAlignment="Center" Height="20" VerticalAlignment="Center" Width="20" Source="MVVM/VIEW/IMAGES/Shape57.png" Stretch="Uniform" />
                       <TextBlock Foreground="DarkGray" Text="{Binding ELEM_BEZ_LONG}" FontWeight="Normal" FontStyle="Italic"/>
                </StackPanel>
            </DataTemplate>
        </TreeView.Resources>
    </TreeView>

VB.NET - MainWindow

    Public Sub New()
        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.

    End Sub

     Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded

        Dim elementsContext As New XELLEntities()
        Tree.DataContext = elementsContext.XELL_DIMENSION.Include("XELL_ELEMS").ToList()
        Tree.ItemsSource = elementsContext.XELL_DIMENSION.Where(Function(y) y.DIMEN_PARENT_ID Is Nothing).ToList()


    End Sub

VB.NET TemplateSelection

    Public Class LeafDataTemplateSelector
    Inherits DataTemplateSelector
    Public Overrides Function SelectTemplate(item As Object, container As DependencyObject) As DataTemplate
        Dim element As FrameworkElement = TryCast(container, FrameworkElement)

        If element IsNot Nothing AndAlso item IsNot Nothing Then

            If TypeOf item Is XELL_DIMENSION Then
                Return TryCast(element.FindResource("Dimension"), DataTemplate)
            ElseIf TypeOf item Is XELL_ELEMENT Then
                Return TryCast(element.FindResource("Element"), DataTemplate)
            End If
        End If

        Return Nothing
    End Function
End Class

VB.NET层次结构转换器

Class HierarchyConverter : Implements IValueConverter
    Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
        Dim node = TryCast(value, Employee)
        If node IsNot Nothing Then
            Return node.Subordinates.Where(Function(i) i.ManagerID = node.EmployeeID).ToList()
        Else
        End If
    End Function 
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
        Throw New NotSupportedException
    End Function End Class

希望这有助于某人;)