Модераторы: gambit, Kefir, Partizan
  

Поиск:

Ответ в темуСоздание новой темы Создание опроса
> WPF один Treeview HierarchicalDataTemplate 
:(
    Опции темы
LoLight
Дата 15.11.2013, 07:02 (ссылка) | (нет голосов) Загрузка ... Загрузка ... Быстрая цитата Цитата


Новичок



Профиль
Группа: Участник
Сообщений: 13
Регистрация: 23.12.2006

Репутация: нет
Всего: нет



Мне нужен Treeview с Book, в который входят Programming, Cooking, Other и Disc, в котором CD и DVD
Как сделать такой Treeview?
Book
  Programming
  Cooking
  Other
Disc
 CD
  DVD
Но пока реализовал:

Book
Programming
Cooking
Other
Disc
Не могу раскрыть второй datarelation


Код

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');


Код

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>



Это сообщение отредактировал(а) LoLight - 15.11.2013, 07:32
PM MAIL   Вверх
AntiInt
Дата 19.11.2013, 06:50 (ссылка) | (нет голосов) Загрузка ... Загрузка ... Быстрая цитата Цитата


Шустрый
*


Профиль
Группа: Участник
Сообщений: 145
Регистрация: 15.5.2009

Репутация: нет
Всего: нет



Дружище, читай личку.
PM MAIL   Вверх
  
Ответ в темуСоздание новой темы Создание опроса
1 Пользователей читают эту тему (1 Гостей и 0 Скрытых Пользователей)
0 Пользователей:
« Предыдущая тема | WPF и Silverlight | Следующая тема »


 




[ Время генерации скрипта: 0.0623 ]   [ Использовано запросов: 21 ]   [ GZIP включён ]


Реклама на сайте     Информационное спонсорство

 
По вопросам размещения рекламы пишите на vladimir(sobaka)vingrad.ru
Отказ от ответственности     Powered by Invision Power Board(R) 1.3 © 2003  IPS, Inc.