2016-04-26 11 views
0

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:

enter image description here

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}}"/> 
+0

Wo ist Ihr hier Befehl? – AnjumSKhan

+0

@AnjumSKhan MenuItem.CommandBindings. Siehe

+0

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 –

Antwort

0

Ich habe meine copycommand in den hinter Code Sicht sein.

Ausblick:

public ICommand CopyCommand { get { return _copyCommand; } } 

Dann konnte ich es zugreifen, wenn ich die Instanz Ansicht setzen in der Tag-Eigenschaft Textbox als here angegeben

<views:myView> 

<Grid> 
<TextBox Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type views:myView}}}"> 
      <TextBox.ContextMenu> 
       <ContextMenu DataContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}"> 
         <MenuItem Header="_Copy" Command="{Binding CopyCommand}"/> 
        </ContextMenu> 
      </TextBox.ContextMenu> 
     </TextBox> 
</views:myView> 

</Grid> 
-1

Geben Command in Ihrem MenuItem wie folgt aus:

<MenuItem Command="ApplicationCommands.Copy" .../> 

auch einen Wert für Command Eigenschaft im CommandBinding geben

<MenuItem.CommandBindings> 
    <CommandBinding Command="ApplicationCommands.Copy" CanExecute="CommandBinding_CanExecute" 
        Executed="CommandBinding_Executed" /> 
</MenuItem.CommandBindings> 
+0

Ich habe versucht aber es hat nicht funktioniert. Ich sah Snoop und es enthält die Befehlseigenschaft, aber die Ereignisse sowie der Befehl wird nicht aufgerufen. Ich gehe verrückt über dieses Problem. –

+0

@JanNavarro PLZ sehe meine aktualisierte Antwort. – AnjumSKhan

0

Wenn Sie WPF versuchen eingebaute zu implementieren ApplicationCommands.Copy Eigenschaft alles, was Sie tun müssen, ist

<TextBox> 
    <TextBox.ContextMenu> 
     <ContextMenu> 
       <MenuItem Command="ApplicationCommands.Copy"/> 
     </ContextMenu> 
    </TextBox.ContextMenu> 
</TextBox> 

Da der Befehl dem Textfeld zugeordnet ist, wird es au das Ausführen von Ereignissen und das Ausführen von Ereignissen.

Allerdings, wenn Sie Ihre eigenen benutzerdefinierten Befehl implementieren möchten, müssen Sie wenig ausführliche

public static class MyCommands 
{ 
    public static readonly RoutedUICommand Copy= new RoutedUICommand 
      (
        "Copy", 
        "_Copy", 
        typeof(MyCommands), 
        null 
      ); 

    //You can define more commands here 
} 

private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e) 
{ 
     e.CanExecute = true; //can check with debugger... 
} 

private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e) 
{ 
    //Your Action 
} 

in XAML

<Window.CommandBindings> 
    <CommandBinding Command="local:MyCommands.Copy" CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed" /> 
</Window.CommandBindings> 
<Grid> 

    <TextBox> 
     <TextBox.ContextMenu> 
      <ContextMenu> 
       <MenuItem Command="local:MyCommands.Copy"/> 
      </ContextMenu> 
     </TextBox.ContextMenu> 
    </TextBox> 
</Grid>