我有兩棵樹:爲什麼e.Source依賴於TreeView填充方法?
- fooTree - 由元素,
- barTree - 通過
兩個樹構造具有的MouseRightButtonDown事件,但e.Source類型而不同:
- fooTree - System.Windows.Controls.TreeViewItem
- barTree - System.Windows.Controls .TreeView
爲什麼e.Source不同?另外,如何獲取barTree的點擊項目?
標記:
<TreeView Name="fooTree" MouseRightButtonDown="fooTree_MouseDown">
<TreeViewItem Header="foo"></TreeViewItem>
<TreeViewItem Header="foo"></TreeViewItem>
</TreeView>
<TreeView Name="barTree" MouseRightButtonDown="barTree_MouseDown" ItemsSource="{Binding BarItems}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate>
<TextBlock Text="{Binding}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
代碼:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
this.DataContext = this;
}
public string[] BarItems
{
get { return new string[] { "bar", "bar" }; }
}
private void barTree_MouseDown(object sender, MouseButtonEventArgs e)
{
}
private void fooTree_MouseDown(object sender, MouseButtonEventArgs e)
{
}
}
+1一個很好的解決方案。 – 2011-03-10 01:08:30