Bonjour Mes amis développeurs;
Mon TreeView contient initialement certains items, et j'ai une Class MyDesign.Command.cs ,cette classe contient les méthods a exécuter pour un ContextMenu associe a un canvas ; maintenant je veux que lorsque l'utilisateur clique sur une méthode dans le ContextMenu, le TreeView se mettre a jour.
Par exemple: Mon TreeView contient initialement:
*Item
|-item1.1
|-item1.1
et je veux lorsque l'utilisateur clique sur la méthode Search dans le ContextMenu et qui est définie dans la classe MyDesign.Command.cs le TreeView se mettre a jour dynamiquement soit en ajoutant des items au TreeView, soit en supprimant des items.. tout ça sans que j'instancié mon MainWindow et l'afficher a nouveau.
Voici Mon code:
MainWindow.xaml
MenuItem.cs
Code : 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
39
40
41
42
43
44
45 <Window x:Class="TreeViewAndDataBanding.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:self="clr-namespace:TreeViewAndDataBanding" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <ContextMenu x:Key="MyDesignContextMenu"> <MenuItem Header="Paste" Command="{x:Static ApplicationCommands.Paste}"/> <MenuItem Header="Search" Command="{x:Static self:MyDesign.Search}"/> </ContextMenu> <self:test x:Key="test"/> </Window.Resources> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="24*"/> <ColumnDefinition Width="23*"/> </Grid.ColumnDefinitions> <TreeView x:Name="MyToolBox" ItemsSource="{StaticResource test}" Grid.Column="1" > <TreeView.ItemTemplate> <DataTemplate> <TreeViewItem Header="All Cars"> <TreeViewItem Header="{Binding Path= Voiture}"> <TreeViewItem Header="{Binding Path=Vitesse}"></TreeViewItem> </TreeViewItem> </TreeViewItem> </DataTemplate> </TreeView.ItemTemplate> </TreeView> <s:MyDesign Focusable="true" x:Name="MyDesigner" Background="{StaticResource WindowBackgroundBrush}" Margin="10" FocusVisualStyle="{x:Null}" ContextMenu="{StaticResource MyDesignContextMenu}" Grid.Coulum="0"/> </Grid> </Window>
test.cs
Code : 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 using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TreeViewAndDataBanding { public class MenuItem { private string _Voiture; private string _Vitesse; public MenuItem( string Voiture,string Vitesse) { this._Voiture = Voiture; this._Vitesse = Vitesse; } public string Voiture { get { return _Voiture; } } public string Vitesse { get { return _Vitesse; } } } }
En dernier Voici la classe dans laquelle je veux mettre a jour mon TreeView
Code : 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 using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace TreeViewAndDataBanding { public class test : ObservableCollection<MenuItem> { public test() { Add(new MenuItem("Rapide", "Ferrari F430")); } } }
MyDesign.Command.cs
Code : 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
39
40
41
42
43
44
45
46
47 using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; namespace TreeViewAndDataBanding { public class MyDesign { public static RoutedCommand Search = new RoutedCommand(); public MyDesign() { this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Paste, Paste_Executed)); this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Paste, Search_Executed)); } private void Search_Executed(object sender, ExecutedRoutedEventArgs e) { // d'ici je veux modifier mon TreeView /******************************************************************************************/ /****** ici je veux modifier et mettre a jour mon Treeview dans l'interface *************/ /****************************************************************************************/ } private void Paste_Executed(object sender, ExecutedRoutedEventArgs e) { } } }
Avez vous une idée svp ??
Partager