如何在上下文菜单上的树视图项中获取控件单击

时间:2013-05-13 10:36:01

标签: c# wpf

我必须从树视图项重命名和删除项(堆栈面板) 我的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>

我正在以编程方式将图像和文本框添加到上面的树视图项目中,如下所示

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

如何从树视图项中删除以编程方式添加的堆栈面板(在删除操作时)?

重命名功能我必须执行textbox属性readonly false,

如何在重命名单击上下文菜单时获取每个堆栈面板中存在的树视图项和控件的每个堆栈面板?

1 个答案:

答案 0 :(得分:0)

您可以使用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);
}

或者您可以使用TreeView.SelectedItem来获取所选项目。