我必须从树视图项我的 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,
如何在重命名上下文菜单时获得每个堆栈面板中存在的树视图项和控件的每个堆栈面板?