WPF TreeView 绑定命令并传递单击的元素
WPF TreeView binding command and passing which element was clicked on
如何将命令绑定到 TreeView 中的元素。我有 MainWindowViewModel 和 TreeViewCommand,我试图在元素周围添加按钮,但不会调用命令。有没有其他方法可以调用命令并传递单击的元素?
<TreeView x:Name="MainTreeView" HorizontalAlignment="Stretch" Margin="10" VerticalAlignment="Stretch" ItemsSource="{Binding Departments}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Positions}" DataType="{x:Type VM:Department}">
<Button Command="{Binding TreeViewCommand}" CommandParameter="{Binding DepartmentName}" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" >
<Label Content="{Binding DepartmentName}"/>
</Button>
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Employees}" DataType="{x:Type VM:Position}">
<Button BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" >
<Label Content="{Binding PositionName}"/>
</Button>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate DataType="{x:Type VM:Employee}">
<Button BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" >
<Label Content="{Binding EmployeeName}"/>
</Button>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
我的 MainWindowViewMode 是
public class MainWindowViewModel : ViewModelBase
{
private List<Department> departments;
public MyICommand<string> TreeViewCommand { get; private set; }
public MainWindowViewModel()
{
TreeViewCommand = new MyICommand<string>(myTreeViewCommand);
Departments = new List<Department>()
{
new Department("DotNet"),
new Department("PHP")
};
}
public List<Department> Departments
{
get
{
return departments;
}
set
{
departments = value;
OnPropertyChanged("Departments");
}
}
public void myTreeViewCommand(string par)
{
Console.ReadKey();
}
}
由于您的 TreeViewCommand 在您的 TreeView 范围内,它具有 Departments 对象,因为它是 DataContext,因此它找不到命令。您应该显式定义定义 TreeViewCommand 的 DataContext。执行以下操作:
<!-- Note the ElementName and Path=DataContext. -->
<Button Command="{Binding ElementName=MainTreeView, Path=DataContext.TreeViewCommand}" CommandParameter="{Binding DepartmentName}" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" >
<Label Content="{Binding DepartmentName}"/>
</Button>
XAML 所有节点:
<TreeView x:Name="MainTreeView" HorizontalAlignment="Stretch" Margin="10" VerticalAlignment="Stretch" ItemsSource="{Binding Departments}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Positions}">
<Button Command="{Binding ElementName=MainTreeView, Path=DataContext.TreeViewCommand}" CommandParameter="{Binding DepartmentName}" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" >
<Label Content="{Binding DepartmentName}"/>
</Button>
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Employees}">
<Button Command="{Binding ElementName=MainTreeView, Path=DataContext.TreeViewCommand}" CommandParameter="{Binding PositionName}" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" >
<Label Content="{Binding PositionName}"/>
</Button>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<Button Command="{Binding ElementName=MainTreeView, Path=DataContext.TreeViewCommand}" CommandParameter="{Binding EmployeeName}" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" >
<Label Content="{Binding EmployeeName}"/>
</Button>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
注意!如果您最终想要传递对象而不是字符串(节点名称),则必须为每个按钮定义 ICommands。
如何将命令绑定到 TreeView 中的元素。我有 MainWindowViewModel 和 TreeViewCommand,我试图在元素周围添加按钮,但不会调用命令。有没有其他方法可以调用命令并传递单击的元素?
<TreeView x:Name="MainTreeView" HorizontalAlignment="Stretch" Margin="10" VerticalAlignment="Stretch" ItemsSource="{Binding Departments}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Positions}" DataType="{x:Type VM:Department}">
<Button Command="{Binding TreeViewCommand}" CommandParameter="{Binding DepartmentName}" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" >
<Label Content="{Binding DepartmentName}"/>
</Button>
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Employees}" DataType="{x:Type VM:Position}">
<Button BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" >
<Label Content="{Binding PositionName}"/>
</Button>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate DataType="{x:Type VM:Employee}">
<Button BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" >
<Label Content="{Binding EmployeeName}"/>
</Button>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
我的 MainWindowViewMode 是
public class MainWindowViewModel : ViewModelBase
{
private List<Department> departments;
public MyICommand<string> TreeViewCommand { get; private set; }
public MainWindowViewModel()
{
TreeViewCommand = new MyICommand<string>(myTreeViewCommand);
Departments = new List<Department>()
{
new Department("DotNet"),
new Department("PHP")
};
}
public List<Department> Departments
{
get
{
return departments;
}
set
{
departments = value;
OnPropertyChanged("Departments");
}
}
public void myTreeViewCommand(string par)
{
Console.ReadKey();
}
}
由于您的 TreeViewCommand 在您的 TreeView 范围内,它具有 Departments 对象,因为它是 DataContext,因此它找不到命令。您应该显式定义定义 TreeViewCommand 的 DataContext。执行以下操作:
<!-- Note the ElementName and Path=DataContext. -->
<Button Command="{Binding ElementName=MainTreeView, Path=DataContext.TreeViewCommand}" CommandParameter="{Binding DepartmentName}" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" >
<Label Content="{Binding DepartmentName}"/>
</Button>
XAML 所有节点:
<TreeView x:Name="MainTreeView" HorizontalAlignment="Stretch" Margin="10" VerticalAlignment="Stretch" ItemsSource="{Binding Departments}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Positions}">
<Button Command="{Binding ElementName=MainTreeView, Path=DataContext.TreeViewCommand}" CommandParameter="{Binding DepartmentName}" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" >
<Label Content="{Binding DepartmentName}"/>
</Button>
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Employees}">
<Button Command="{Binding ElementName=MainTreeView, Path=DataContext.TreeViewCommand}" CommandParameter="{Binding PositionName}" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" >
<Label Content="{Binding PositionName}"/>
</Button>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<Button Command="{Binding ElementName=MainTreeView, Path=DataContext.TreeViewCommand}" CommandParameter="{Binding EmployeeName}" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" >
<Label Content="{Binding EmployeeName}"/>
</Button>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
注意!如果您最终想要传递对象而不是字符串(节点名称),则必须为每个按钮定义 ICommands。