我有一個ContextMenu出現在TreeViewItem的右鍵單擊上。我現在想要做的是將關於TreeViewItem的一些細節傳遞給上下文菜單。將TreeViewItem的細節綁定到ContextMenu
我會怎麼做,從這裏開始的:
XAML
<TreeView x:Class="MyApp.TreeControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
PreviewMouseLeftButtonDown="TreeView_PreviewMouseLeftButtonDown"
PreviewMouseMove="TreeView_PreviewMouseMove"
PreviewMouseRightButtonDown="TreeControl_OnPreviewMouseRightButtonDown">
<TreeView.ContextMenu>
<ContextMenu>
<MenuItem Name="dataItem1" Header="Property1"></MenuItem>
<MenuItem Name="dataItem2" Header="Property2"></MenuItem>
<Separator/>
<MenuItem Name="taskItem" Header="Tasks" Click="TaskItem_OnClick"></MenuItem>
</ContextMenu>
</TreeView.ContextMenu>
</TreeView>
我想Property1
和Property2
在馬上就是點擊樹型視圖這些屬性的值替換的話,那種像:{binding selectedItem.Property1}
代碼:(基本上以突出顯示點擊的節點)
private void TreeControl_OnPreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
TreeViewItem treeViewItem = VisualUpwardSearch(e.OriginalSource as DependencyObject);
if (treeViewItem != null)
{
treeViewItem.IsSelected = true;
treeViewItem.Focus();
e.Handled = true;
}
}