如何使控件出现在上下文菜单上单击的树状视图项中
本文关键字:单击 视图 菜单 控件 何使 上下文 | 更新日期: 2023-09-27 18:13:13
我必须重命名并删除树视图项中的项(堆栈面板(我的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);
}
如何从树视图项中删除程序添加的堆栈面板(在删除操作中(?
对于重命名功能,我必须执行文本框属性readonlyfalse,
如何在上下文菜单的重命名单击时获得树视图项的每个堆栈面板和每个堆栈面板中的控件?
您可以像这样使用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
来获取所选项目。