Ich sah diese SO question aber wenn ich es versuchte es funktioniert nicht.WPF Befehl Bindung gebrochen
Ich weiß, dass ich mvvm tun kann, aber was der Befehl ausführen tut, ist spezifisch, also wollte ich, dass es in der Ansicht gemacht wird.
<Grid>
<TextBox>
<TextBox.ContextMenu>
<ContextMenu>
<MenuItem Header="_Copy" CommandTarget="{Binding Path=PlacementTarget,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ContextMenu}}}">
<MenuItem.CommandBindings>
<CommandBinding CanExecute="CommandBinding_CanExecute"
Executed="CommandBinding_Executed"
/>
</MenuItem.CommandBindings>
</MenuItem>
</ContextMenu>
</TextBox.ContextMenu>
</TextBox>
</Grid>
-Code hinter:
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = false;
}
Ich erwarte das Menü deaktiviert sein, aber es ist nicht:
ich andere Ansätze versucht:
<!--Approach 2: CopyCommand as property of the Window. CopyCommand is in code-behind of the window-->
<MenuItem Header="_Copy" Command="{Binding Path=CopyCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
<!--Approach 3: Saw other samples on the net about binding to PlacementTarget. CopyCommand is in code-behind of the window-->
<MenuItem Header="_Copy" Command="{Binding Path=PlacementTarget.CopyCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
Wo ist Ihr hier Befehl? – AnjumSKhan
@AnjumSKhan MenuItem.CommandBindings. Siehe
Siehe auch: http://stackoverflow.com/questions/36855188/wpf-command-binding-broken und http://stackoverflow.com/questions/4096186/Warum-ist-nicht-gut-zu-expose-Modell-durch-Viewmodel-in-silverlight-mvvm/4154225 # 4154225 –