1 / 1 / 0
Регистрация: 23.11.2010
Сообщений: 136
|
|
1
|
WPF один Treeview и раличные HierarchicalDataTemplate в нем
14.11.2013, 12:30. Показов 1568. Ответов 0
Мне нужен Treeview с Book, в который входят Programming, Cooking, Other и Disc, в котором CD и DVD
Book
Programming
Cooking
Other
Disc
CD
DVD
Но пока получается:
Book
Programming
Cooking
Other
Disc
Код приведен. Как сделать как наверху?
SQL | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| 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'); |
|
C# | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| namespace Test
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
try
{
this.GoodTreeView.DataContext = Test.CreateDataRelation();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
} |
|
C# | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
| 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
}
} |
|
XML | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
| <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> |
|
0
|