简体   繁体   English

WPF一个Treeview和其中的其他HierarchicalDataTemplate

[英]WPF One Treeview and different HierarchicalDataTemplate in it

I want TreeView 我想要TreeView

Books that can be Programming, Cooking, Other. 可以是编程,烹饪或其他书籍。 Discs that can be CD or DVD. 可以是CD或DVD的光盘。 Books and Disc have Good_type_id.There must be strucrure like in database I created. 书籍和光盘具有Good_type_id。必须具有与我创建的数据库类似的结构。

Book
  Programming
  Cooking
  Other
Disc
  CD 
  DVD

There would be another type of discs and some extra params for books but not in this topic. 将会有另一种类型的光盘和一些书的附加参数,但本主题中没有。 My result is 我的结果是

Book
  Programming
  Cooking
  Other
Disc

I want HierarchicalDataTemplate not only for book type but for disc type. 我不仅要针对书籍类型而且要针对光盘类型使用HierarchicalDataTemplate。 I do not know how to do it in one treevie. 我不知道如何在一棵树上做。 Here is code. 这是代码。 In treeview would be some nested items or join sql code but not in this topic. 在treeview中将是一些嵌套项或联接sql代码,但不在本主题中。 I create 2 relations. 我创建2个关系。 One for books through [Good_Type_Id] works. 一本通过[Good_Type_Id]的书籍有效。 I wanna the same for discs in one treeview. 我希望在一个树状视图中使用相同的光盘。

CREATE TABLE [dbo].[Book_type] (
[Good_Type_Id] [varchar] (50) NOT NULL,
[Book_Type_Id] [varchar] (50) NOT NULL,
[Book_Type_Name] [varchar] (50) NOT NULL,
) ON [PRIMARY]
GO

INSERT INTO [dbo].[Book_type] VALUES  ('1','1b','Programming');
INSERT INTO [dbo].[Book_type] VALUES  ('1','2b','Cooking');
INSERT INTO [dbo].[Book_type] VALUES  ('1','3b','Other_Book');


CREATE TABLE [dbo].[Disc_type] (
[Good_Type_Id] [varchar] (50) NOT NULL,
[Disc_Type_Id] [varchar] (50) NOT NULL,
[Disc_Type_Name] [varchar] (50) NOT NULL,
) ON [PRIMARY]
GO

INSERT INTO [dbo].[Disc_type] VALUES  ('2','1d','CD');
INSERT INTO [dbo].[Disc_type] VALUES  ('2','2d','DVD');



namespace Test
{

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            try
            {
                this.GoodTreeView.DataContext = Test.CreateDataRelation();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message); 
            }
        }
    }
}
    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Test
{
    public static class Test
    {
        #region

        /// <summary>
        /// Data Relation Between Two Tables Such as good_type and Goods
        /// </summary>
        /// <returns>DataSet</returns>
        public static DataSet CreateDataRelation()
        {
            try
            {
                SqlConnection SqlCon = new SqlConnection();
                SqlCon.ConnectionString = @"Data Source=(local);DATABASE=Test;Integrated Security=SSPI";
                SqlCon.Open();

                //SqlDataAdapter Sql_Good_Type_Da = new SqlDataAdapter("SELECT Good_Type_Id,Good_Type_Name from Good_Type", SqlCon);
                SqlDataAdapter Sql_Good_Type_Da = new SqlDataAdapter("SELECT * from Good_Type", SqlCon);
                //SqlDataAdapter Sql_Book_Type_Da = new SqlDataAdapter("SELECT Good_type_Id, Book_Type_Id, Book_type_Name from Book_type", SqlCon);
                SqlDataAdapter Sql_Book_Type_Da = new SqlDataAdapter("SELECT * FROM Book_type", SqlCon);
                SqlDataAdapter Sql_Disc_Type_Da = new SqlDataAdapter("SELECT * FROM Disc_type", SqlCon);
                DataSet Ds = new DataSet();
                Sql_Good_Type_Da.Fill(Ds, "Good_Type");
                Sql_Book_Type_Da.Fill(Ds, "Book_Type");
                Sql_Disc_Type_Da.Fill(Ds, "Disc_Type");


                DataRelation Dr_Good_Book = new DataRelation
                    ("Good_Book_Relation",
                    Ds.Tables["Good_Type"].Columns["Good_Type_Id"],
                    Ds.Tables["Book_Type"].Columns["Good_Type_Id"],
                    true
                    );

                DataRelation Dr_Good_Disc = new DataRelation
                    ("Good_Disc_Relation",
                    Ds.Tables["Good_Type"].Columns["Good_Type_Id"],
                    Ds.Tables["Disc_Type"].Columns["Good_Type_Id"],
                    true
                    );


                Dr_Good_Book.Nested = true;
                Dr_Good_Disc.Nested = true;

                Ds.Relations.Add(Dr_Good_Book);
                Ds.Relations.Add(Dr_Good_Disc);

                return Ds;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        #endregion
    }
}

<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate x:Key="Book_Type_DataTemplate">
            <Grid Width="800" Height="20">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="0.771*"/>
                    <ColumnDefinition Width="0.229*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="0.5*"/>
                </Grid.RowDefinitions>
                <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Good_Type_Id}" Margin="0,0" Foreground="Black"></TextBlock>
                <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Book_Type_Id}" Margin="100,0" Foreground="Black"></TextBlock>
                <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Book_Type_Name}" Margin="200,0" Foreground="Black"></TextBlock>             
            </Grid>              
        </DataTemplate>

        <DataTemplate x:Key="Disc_Type_DataTemplate">
            <Grid Width="800" Height="20">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="0.771*"/>
                    <ColumnDefinition Width="0.229*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="0.5*"/>
                </Grid.RowDefinitions>
                <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Good_Type_Id}" Margin="0,0" Foreground="Black"></TextBlock>
                <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Disc_Type_Id}" Margin="100,0" Foreground="Black"></TextBlock>
                <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Disc_Type_Name}" Margin="200,0" Foreground="Black"></TextBlock>
            </Grid>
        </DataTemplate>


        <HierarchicalDataTemplate x:Key="Good_Book_DataTemplate" 
                      ItemsSource="{Binding Good_Book_Relation}" 
                      ItemTemplate="{StaticResource Book_Type_DataTemplate}">       
            <Grid Height="20">
                <TextBlock Text="{Binding Good_Type_Name}" Margin="10,0" HorizontalAlignment="Stretch" VerticalAlignment="Center"></TextBlock>    
                </Grid>     
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate x:Key="Good_Disc_DataTemplate" 
                      ItemsSource="{Binding Good_Disc_Relation}" 
                      ItemTemplate="{StaticResource Disc_Type_DataTemplate}">
            <Grid Height="20">
                <TextBlock Text="{Binding Good_Type_Name}" Margin="10,0" HorizontalAlignment="Stretch" VerticalAlignment="Center"></TextBlock>
            </Grid>
        </HierarchicalDataTemplate>


    </Window.Resources>
    <Grid>

        <TreeView x:Name="GoodTreeView" ItemsSource="{Binding Good_Type}" ItemTemplate="{DynamicResource Good_Book_DataTemplate}" 
                  HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">          
        </TreeView>


    </Grid>
</Window>

I think you are coupling your data objects and your view objects far too closely together. 我认为您将数据对象和视图对象紧密地耦合在一起。 have a look at the MVVM pattern. 看一下MVVM模式。

At the moment you have models that come back from the data access calls, and you are binding your view directly onto them. 目前,您有一些从数据访问调用返回的模型,并且将视图直接绑定到它们。 You need to introduce something to translate between the two tiers. 您需要引入一些东西在两层之间进行翻译。 In this way you can have DiscViewModel s and BookViewModel s and have different data templates for each. 这样,您可以拥有DiscViewModelBookViewModel并且每个都有不同的数据模板。

You might even want to define 您甚至可能想要定义

class BookViewModel : BaseViewModel {}
class DiscViewModel : BaseViewModel {}

class BaseViewModel
{
  // All properties defined here
}

as the WPF type engine will still distinguish between the two classes even if they are identical. 因为WPF类型引擎即使它们是相同的,仍将区分这两个类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM