WPF:向动态创建的树添加上下文菜单

时间:2010-12-27 14:43:23

标签: c# wpf treeview

大家好我想把ContexMenu添加到我动态生成的树中。

以下是我的生成树的代码。

我需要添加ContexMenu:

新: 编辑: DELETE

在MouseClick上我应该能够执行单击各个按钮的相同操作。

任何机构都可以帮助完成代码。

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";
        }

编辑:

我正在寻找一个解决方案,我应该能够根据Header信息将ContexMenu添加或限制为TreeItem。

1 个答案:

答案 0 :(得分:3)

您可以使用样式设置常见的ContextMenu属性:

<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>

或者您可以在代码中添加上下文菜单:行this.MYTree.Items.Add(Case);之前 你可以添加这样的东西:

var menu = new ContextMenu();
menu.Children.Add(new MenuItem{Header = "Save"});
menu.Children.Add(new MenuItem{Header = "Load"});
Case.ContextMenu = menu;