简体   繁体   English

如何使控件出现在上下文菜单的树视图项目中

[英]How to get controls present in tree view item on context menu click

I have to rename and delete item(stack panel) from the tree view item My XAML structure as follows 我必须重命名和删除树视图项目My XAML结构中的项目(堆栈面板),如下所示

<TreeViewItem Name="trvMyCollections" Header="MY COLLECTIONS" 
              Foreground="#8A949E" Background="#DCE1E7" >
    <TreeViewItem.ContextMenu>
        <ContextMenu>
            <MenuItem Header="New Collection" Name="mniNewCollection"
                      Click="mniNewCollection_Click"></MenuItem>
            <Separator></Separator>
            <MenuItem Header="Rename" Name="mniRenameCollection"
                      Click="mniRenameCollection_Click"></MenuItem>
            <MenuItem Header="Move to Trash" Name="mniMoveToThrash"
                      Click="mniMoveToThrash_Click"></MenuItem>
        </ContextMenu>
    </TreeViewItem.ContextMenu>
</TreeViewItem>

I am programatically adding image and textbox to above treeview item as follows 我以编程方式将图像和文本框添加到上述树视图项中,如下所示

private void CreateCollectionUI(string collectionId, string collectionName)
{
    StackPanel StackPanelCollection = new StackPanel();
    StackPanelCollection.Width = 290;
    StackPanelCollection.Height = 28;
    StackPanelCollection.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#DCE1E7"));
    StackPanelCollection.Orientation = Orientation.Horizontal;
    StackPanelCollection.Margin = new Thickness(-39, 0, 0, 0);

    Image imgCollectionImage = new Image();
    imgCollectionImage.Source = new BitmapImage(new Uri(@"pack://application:,,,/component/Resources/Images/folder.png"));
    imgCollectionImage.Margin = new Thickness(38, 0, 0, 0);
    imgCollectionImage.Height = 23;
    imgCollectionImage.Width = 23;
    imgCollectionImage.Name = string.Concat("img", collectionId);
    StackPanelCollection.Children.Add(imgCollectionImage);

    TextBox txbxCollectionName = new TextBox();
    txbxCollectionName.Text = collectionName;
    txbxCollectionName.Background = Brushes.Transparent;
    txbxCollectionName.BorderThickness = new Thickness(0);
    txbxCollectionName.IsReadOnly = true;
    txbxCollectionName.HorizontalAlignment = HorizontalAlignment.Center;
    txbxCollectionName.VerticalAlignment = VerticalAlignment.Center;
    txbxCollectionName.Foreground = Brushes.Black;
    txbxCollectionName.Margin = new Thickness(10, 0, 0, 0);
    txbxCollectionName.LostFocus += new RoutedEventHandler(txbxCollectionName_LostFocus);
    txbxCollectionName.MouseDown += new MouseButtonEventHandler(StackPanelCollection_MouseDown);
    StackPanelCollection.Children.Add(txbxCollectionName);
    txbxCollectionName.Tag = collectionId;
    StackPanelCollection.Name = string.Concat("stpnlCollection", collectionId);
    StackPanelCollection.MouseDown += new MouseButtonEventHandler(StackPanelCollection_MouseDown);
    trvMyCollections.Items.Add(StackPanelCollection);
}

How to remove programatically added stack panel from tree view item(on delete operation)? 如何从树视图项中删除以编程方式添加的堆栈面板(在删除操作时)?

for rename functionality I have to do the textbox property readonly false, 对于重命名功能,我必须将textbox属性设置为readonly false,

How can I get the each stack panel of treeview item and control present in each stack panel on rename click of context menu? 在上下文菜单中重命名单击时,如何获取每个TreeView项目的堆栈面板和控件在每个堆栈面板中?

You can use FindName method like this: 您可以这样使用FindName方法:

string panelName = string.Concat("stpnlCollection", collectionId);
StackPanel panel = trvMyCollections.FindName(panelName) as StackPanel;

if (panel != null)
{
   //do your stuff, for example, delete:
   trvMyCollections.Items.Remove(panel);
}

Or you can use TreeView.SelectedItem to get selected item. 或者,您可以使用TreeView.SelectedItem获取所选项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM