WPF: Adding ContexMenu to Dynamically Created Tree
Hi all I am tryi开发者_开发技巧ng to add ContexMenu into my Dynamically generated Tree.
Below is my Code For generating Tree.
I need to add ContexMenu for :
NEW: EDIT: DELETE
on MouseClick I should be able to perform the same operation of clicking the Respective Buttons.
Can any body help in completing the code.
XML:CODE
<Window x:Class="NewTree_DynamicNode.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"
Loaded="TestsTreeViewPageFunction_Loaded">
<Grid>
<TreeView Name="treeFileSystem" >
<TreeViewItem Header="Suite" Name="MYTree" Tag="hi" IsExpanded="True">
<TreeViewItem Name="treeFileSystem1" />
</TreeViewItem>
</TreeView>
<TextBox Name="textBox1" Height="23" HorizontalAlignment="Left" Margin="121,150,0,0" VerticalAlignment="Top" Width="120" />
<Button Content="New" Height="23" HorizontalAlignment="Left" Margin="12,121,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="New_Click" />
<Button Content="Edit" Height="23" HorizontalAlignment="Left" Margin="12,150,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="Edit_Click"/>
<Button Content="Copy" Height="23" HorizontalAlignment="Left" Margin="12,179,0,0" Name="button3" VerticalAlignment="Top" Width="75" Click="Copy_Click"/>
</Grid>
</Window>
C# CODE:
private void TestsTreeViewPageFunction_Loaded(object sender,
RoutedEventArgs e)
{
this.MYTree.Items.Clear();
for (int j = 1; j < 4; j++)
{
TreeViewItem Case = new TreeViewItem();
Case.Header = "Case "+j.ToString();
Case.IsExpanded = true;
Case.Items.Add(Trythis());
this.MYTree.Items.Add(Case);
}
}
private TreeViewItem Trythis()
{
TreeViewItem Step = new TreeViewItem();
for (int i = 0; i < 5; i++)
{
Step.Header = "Steps " + i.ToString();
}
return Step;
}
private void New_Click(object sender, RoutedEventArgs e)
{
textBox1.Text = "New Button Clicked";
}
private void Edit_Click(object sender, RoutedEventArgs e)
{
textBox1.Text = "Edit Button Clicked";
}
private void Copy_Click(object sender, RoutedEventArgs e)
{
textBox1.Text = "Copy Button Clicked";
}
EDIT:
I am looking for a solution where I should be able to add or limit the ContexMenu to TreeItem based on there Header information.
You can use style to set common ContextMenu property:
<TreeViewItem Header="Suite" Name="MYTree" Tag="hi" IsExpanded="True">
<TreeViewItem.Resources>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="New" />
<MenuItem Header="Edit" />
<MenuItem Header="Delete" />
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</TreeViewItem.Resources>
<TreeViewItem x:Name="treeFileSystem1" />
</TreeViewItem>
Or you can add context menu in code: before line this.MYTree.Items.Add(Case);
you can add something like this:
var menu = new ContextMenu();
menu.Children.Add(new MenuItem{Header = "Save"});
menu.Children.Add(new MenuItem{Header = "Load"});
Case.ContextMenu = menu;
精彩评论