gpt4 book ai didi

c# - TreeView 的 HierarchicalDataTemplate 中的 KeyBinding

转载 作者:行者123 更新时间:2023-12-03 10:16:07 25 4
gpt4 key购买 nike

我有一个 TreeView .我想启用 EditLeafCommand点击 F2 .

型号:

public class Leaf
{
public string LeafName { get; set; }
public bool HasChildren { get; set; }
}

窗口 View 模型:
public MainWindowViewModel{

public ReadOnlyCollection<LeafViewModel> Leafs
{
get { return leafs; }
}
}

TreeView的ViewModel:
public class LeafViewModel : TreeViewItemViewModel
{
public ObservableCollection<TreeViewItemViewModel> Children
{
get { return _children; }
}
public string LeafName { get; set; }
public RelayCommand EditLeafCommand { get; set; }

}

XAML:
<TreeView  ItemsSource="{Binding Leafs}">               
<TreeView.InputBindings>
<KeyBinding Key="F2" Command="{Binding SelectedItem.EditLeafCommand,
diag:PresentationTraceSources.TraceLevel=High}"/>
</TreeView.InputBindings>
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type vm:LeafViewModel}"
ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding LeafName}" IsReadOnly="{Binding IsReadOnlyItem}"
Tag="{Binding DataContext, RelativeSource={RelativeSource Self}}">
<TextBox.ContextMenu>
<ContextMenu DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
<MenuItem Command="{Binding EditLeafCommand}" CommandParameter="{Binding ALeaf}" Header="Edit" />
</ContextMenu>
</TextBox.ContextMenu>
</TextBox>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>

输出窗口 :

System.Windows.Data Warning: 56 : Created BindingExpression (hash=2683661) for Binding (hash=47044325)

System.Windows.Data Warning: 58 : Path: 'EditLeafCommand'

System.Windows.Data Warning: 60 : BindingExpression (hash=2683661): Default mode resolved to OneWay

System.Windows.Data Warning: 61 : BindingExpression (hash=2683661): Default update trigger resolved to PropertyChanged

System.Windows.Data Warning: 62 : BindingExpression (hash=2683661): Attach to System.Windows.Input.KeyBinding.Command (hash=29578451)

System.Windows.Data Warning: 64 : BindingExpression (hash=2683661): Use Framework mentor

System.Windows.Data Warning: 67 : BindingExpression (hash=2683661): Resolving source

System.Windows.Data Warning: 69 : BindingExpression (hash=2683661): Framework mentor not found

System.Windows.Data Warning: 65 : BindingExpression (hash=2683661): Resolve source deferred

System.Windows.Data Warning: 95 : BindingExpression (hash=2683661): Got InheritanceContextChanged event from KeyBinding (hash=29578451)

System.Windows.Data Warning: 67 : BindingExpression (hash=2683661): Resolving source

System.Windows.Data Warning: 70 : BindingExpression (hash=2683661): Found data context element: TreeView (hash=11903911) (OK)

System.Windows.Data Warning: 78 : BindingExpression (hash=2683661): Activate with root item MainWindowViewModel (hash=44115416)

System.Windows.Data Warning: 108 : BindingExpression (hash=2683661): At level 0 - for MainWindowViewModel.EditLeafCommand found accessor

System.Windows.Data Error: 40 : BindingExpression path error: 'EditLeafCommand' property not found on 'object' ''MainWindowViewModel' (HashCode=44115416)'. BindingExpression:Path=EditLeafCommand; DataItem='MainWindowViewModel' (HashCode=44115416); target element is 'KeyBinding' (HashCode=29578451); target property is 'Command' (type 'ICommand')

System.Windows.Data Warning: 80 : BindingExpression (hash=2683661): TransferValue - got raw value {DependencyProperty.UnsetValue}

System.Windows.Data Warning: 88 : BindingExpression (hash=2683661): TransferValue - using fallback/default value

System.Windows.Data Warning: 89 : BindingExpression (hash=2683661): TransferValue - using final value



我看到这篇文章和 it is it is the same question ,但是,接受的答案没有任何代码(我试图通过链接,但是,提供的方法对我没有帮助)

错误说:

System.Windows.Data Error: 40 :BindingExpression path error: 'EditLeafCommand' property not found on 'object' ''MainWindowViewModel'



但是我怎样才能直接到 EditLeadViewModel ?

如何调用EditLeafCommand来自 LeafViewModel并发送参数?

最佳答案

您的 MainViewModel没有 SelectedItem属性(property)。您需要将此属性添加到您的 View 模型并确保 INotifyPropertyChanged.PropertyChanged当该属性更改时触发事件。

每当 TreeView 的选定项发生更改时,您还需要更新此属性。有多种方法可以做到这一点。一种方法是使用 Nuget 包 System.Windows.Interactivity.WPF .您必须添加命名空间声明:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

然后,在您的树形 View XAML 元素中添加以下内容(我只显示要添加的内容 - 将 XAML 的其余部分保持在 TreeView 元素内):
<TreeView Name="treeView" ItemsSource="{Binding Leafs}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectedItemChanged">
<i:InvokeCommandAction Command="{Binding SetSelectedItemCommand, PresentationTraceSources.TraceLevel=High}" CommandParameter="{Binding SelectedItem, ElementName=treeView}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TreeView>

请注意 TreeView元素有一个名称( treeView ),用于 CommandParameter InvokeCommandAction 中的绑定(bind)元素。

另外,请注意您必须添加 SetSelectedItemCommandMainViewModel .此命令应设置 SelectedItem我在第一段中描述的属性。下面是一些使用通用 RelayCommand 的代码片段:
class MainWindowViewModel {

TreeViewItemViewModel selectedItem;

public MainWindowViewModel() {
SetSelectedItemCommand = new RelayCommand<TreeViewItemViewModel>(SetSelectedItem);
}

public TreeViewItemViewModel SelectedItem {
get { return selectedItem; }
set {
selectedItem = value;
OnPropertyChanged();
}
}

void SetSelectedItem(TreeViewItemViewModel viewModel) {
SelectedItem = viewModel;
}

}

这是使您的键绑定(bind)到 SelectedItem.EditLeafCommand 所需的基本内容。去工作。但是,您还有另一个问题。您的 HierarchicalDataTemplate将树节点定义为 TextBox .当您点击 TextBox没有点击冒泡到 TreeView并且选择没有改变。我的建议是您使用每个树节点的非交互式表示(例如 TextBlock )。那么,当 EditLeafCommand被调用你放一个 TextBox顶部允许用户编辑节点。

关于c# - TreeView 的 HierarchicalDataTemplate 中的 KeyBinding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36865204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com