0

我必须从树视图项我的 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);
}

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

对于重命名功能,我必须将文本框属性只读为 false,

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

4

1 回答 1

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来获取所选项目。

于 2013-05-14T06:47:26.363 回答