Bonjour à tous,
J'ai débuter le WPF il y a 2-3 semaines. J'ai commencer par terminer de lire mon APress et depuis j'ai quelques petites interrogations sur la ou les bonnes façon de faire.
Prenons par exemple un Treeview qui doit afficher cette structure :
La bonne façon de faire selon moi et ce que j'ai lu, serait donc de créer, dans un resource, les HierarchicalDataTemplate nécessaires à mes différents éléments.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 - Organisation - Personne - Voiture - Voiture - Personne - Organisation - Personne - Organisation - Personne - Voiture
Code xaml : Sélectionner tout - Visualiser dans une fenêtre à part
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 <Window.Resources> <HierarchicalDataTemplate DataType="{x:Type local:Organisation}" ItemsSource="{Binding Path=Personnes}"> <TextBlock Text="{Binding Path=Nom}"></TextBlock> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="{x:Type local:Personne}" ItemsSource="{Binding Path=Voitures}"> <TextBlock Text="{Binding Path=NomComplet}"></TextBlock> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="{x:Type local:Voiture}"> <Grid HorizontalAlignment="Stretch"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"></ColumnDefinition> <ColumnDefinition Width="*"></ColumnDefinition> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Grid.Column="0" Text="Constructeur"></TextBlock> <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=Constructeur}"></TextBlock> <TextBlock Grid.Row="1" Grid.Column="0" Text="Modèle"></TextBlock> <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=Model}"></TextBlock> <TextBlock Grid.Row="2" Grid.Column="0" Text="Année"></TextBlock> <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Path=Annee}"></TextBlock> <TextBlock Grid.Row="3" Grid.Column="0" Text="Prix"></TextBlock> <TextBlock Grid.Row="3" Grid.Column="1" Text="{Binding Converter={StaticResource VoiturePrixConvertisseur}}"></TextBlock> </Grid> </HierarchicalDataTemplate> </Window.Resources>
Comme vous avez pu le remarqué, je n'applique aucun style visuel à mes éléments. Et la raison est justement pourquoi je post un message ici.
Si je veux attribuer des changements visuels (style) à aux éléments à l'intérieur de mon HierarchicalDataTemplate, dois-je m'y prendre comme ceci ?
Code xaml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 <Style x:Key="hierarchicalDataTemplatePersonnesStyle"> <Setter Property="Border.Background" Value="Blue"></Setter> <Setter Property="Border.BorderThickness" Value="1"></Setter> <Setter Property="Border.CornerRadius" Value="4"></Setter> <Setter Property="Border.Padding" Value="2"></Setter> <Setter Property="StackPanel.Margin" Value="2"></Setter> <Setter Property="StackPanel.VerticalAlignment" Value="Center"></Setter> <Setter Property="TextBlock.HorizontalAlignment" Value="Left"></Setter> <Setter Property="TextBlock.Foreground" Value="White"></Setter> </Style> <HierarchicalDataTemplate DataType="{x:Type local:Organisation}" ItemsSource="{Binding Path=Personnes}"> <Border Style="{StaticResource hierarchicalDataTemplatePersonnesStyle}"> <StackPanel> <TextBlock Text="{Binding Path=Nom}"></TextBlock> </StackPanel> </Border> </HierarchicalDataTemplate>
Toutefois, de cette façon, j'ai remarqué qu'autant un Border, qu'un StackPanel et un TexBlock on la propriété Background, donc selon moi, seulement la dernière ligne déclarative sera prise en compte, ais-je bien compris ? Si oui, alors c'est pas l'idéal ?
Ou, dois-je créer un style indépendant, avec des x:key différents, pour le border, un autre pour stack panel, un autre pour le TextBlock et leur attribuer leur style ? Ex :
Code xaml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 <HierarchicalDataTemplate DataType="{x:Type local:Organisation}" ItemsSource="{Binding Path=Personnes}"> <Border Style="{StaticResource StyleBorder}"> <StackPanel Style="{StaticResource StylePannel}"> <TextBlock Style="{StaticResource StyleTextBlock}" Text="{Binding Path=Nom}"></TextBlock> </StackPanel> </Border> </HierarchicalDataTemplate>
Merci beaucoup pour votre aide.
J'aime beaucoup cette technologie jusqu'ici, mais j'ai hâte de la matrîser d'avantage![]()
Partager