我有一個TreeView
。我想通過點擊F2啓用EditLeafCommand
。TreeView的HierarchicalDataTemplate中的KeyBinding
型號:
public class Leaf
{
public string LeafName { get; set; }
public bool HasChildren { get; set; }
}
視圖模型窗口:
public MainWindowViewModel{
public ReadOnlyCollection<LeafViewModel> Leafs
{
get { return leafs; }
}
}
視圖模型的TreeView的:
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
?
如何從LeafViewModel
中調用EditLeafCommand
併發送參數?
我不知道你在哪裏結合SetSelectedItemCommand任何XAML,這似乎是問題的XAML,其中是嗎? –
是的,我可以看到沒有一個名爲SetSelectedItemCommand的屬性,但是在XAML中必須有一個正在執行'{Binding SetSelectedItemCommand}' –
@GlenThomas的地方,請參閱我的更新回答。我更新了信息,我想綁定到'LeafViewModel',而不是'MainViewModel'。他們真的很愚蠢的錯誤。抱歉。 – StepUp