2
在我的WPF應用程序中,我想添加一個TreeView控件。樹視圖控件需要填充來自數據庫的項目。所以我將ItemsSource屬性綁定到字符串集合。 樹形控件中的每個項目可以有0到32個子項目。這些項目再次需要綁定。每個這些子項目都應該有一個上下文菜單,其中有兩個選項「重命名」和「刪除」。我怎麼能在WPF中做到這一點。 任何幫助將不勝感激。提前致謝。在子項目中使用上下文菜單添加WPF中的TreeView
在我的WPF應用程序中,我想添加一個TreeView控件。樹視圖控件需要填充來自數據庫的項目。所以我將ItemsSource屬性綁定到字符串集合。 樹形控件中的每個項目可以有0到32個子項目。這些項目再次需要綁定。每個這些子項目都應該有一個上下文菜單,其中有兩個選項「重命名」和「刪除」。我怎麼能在WPF中做到這一點。 任何幫助將不勝感激。提前致謝。在子項目中使用上下文菜單添加WPF中的TreeView
有幾種方法可以做到這一點。這是一種使用綁定到底層視圖模型上的屬性IsLeaf
的觸發器應用上下文菜單的方法。
MainWindow.xaml:
<Window x:Class="WpfScratch.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window.Resources>
<!-- the context menu for all tree view items -->
<ContextMenu x:Key="TreeViewItemContextMenu">
<MenuItem Header="Rename" />
<MenuItem Header="Delete" />
</ContextMenu>
<!-- the data template for all tree view items -->
<HierarchicalDataTemplate x:Key="TreeViewItemTemplate" ItemsSource="{Binding Nodes}">
<TextBlock x:Name="TextBlock" Text="{Binding Text}" />
<HierarchicalDataTemplate.Triggers>
<DataTrigger Binding="{Binding IsLeaf}" Value="True">
<Setter TargetName="TextBlock" Property="ContextMenu" Value="{StaticResource TreeViewItemContextMenu}" />
</DataTrigger>
</HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>
</Window.Resources>
<!-- the treeview -->
<TreeView DataContext="{Binding TreeView}"
ItemsSource="{Binding Nodes}"
ItemTemplate="{StaticResource TreeViewItemTemplate}">
</TreeView>
</Window>
MainWindow.xaml.cs:
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowModel(
new MainWindowTreeViewModel(
new MainWindowTreeViewNodeModel(
"1",
new MainWindowTreeViewNodeModel("A"),
new MainWindowTreeViewNodeModel("B"),
new MainWindowTreeViewNodeModel("C")),
new MainWindowTreeViewNodeModel(
"2",
new MainWindowTreeViewNodeModel("A"),
new MainWindowTreeViewNodeModel("B"),
new MainWindowTreeViewNodeModel("C")),
new MainWindowTreeViewNodeModel(
"3",
new MainWindowTreeViewNodeModel("A"),
new MainWindowTreeViewNodeModel("B"),
new MainWindowTreeViewNodeModel("C"))));
}
}
MainWindowModel.cs:
public class MainWindowModel
{
public MainWindowModel(MainWindowTreeViewModel treeView)
{
TreeView = treeView;
}
public MainWindowTreeViewModel TreeView { get; private set; }
}
public class MainWindowTreeViewModel
{
public MainWindowTreeViewModel(params MainWindowTreeViewNodeModel[] nodes)
{
Nodes = nodes.ToList().AsReadOnly();
}
public ReadOnlyCollection<MainWindowTreeViewNodeModel> Nodes { get; private set; }
}
public class MainWindowTreeViewNodeModel
{
public MainWindowTreeViewNodeModel(string text, params MainWindowTreeViewNodeModel[] nodes)
{
Text = text;
Nodes = nodes.ToList().AsReadOnly();
}
public string Text { get; private set; }
public ReadOnlyCollection<MainWindowTreeViewNodeModel> Nodes { get; private set; }
public bool IsLeaf { get { return Nodes.Count == 0; } }
}
非常感謝主席先生,幫了很大的:) – WAQ
如何我們現在進行重命名嗎? 我已經通過顯示一個小對話框來完成它,它採用新的名稱並將名稱應用於子節點,但是如何在適當的位置(在樹節點內? – WAQ