2017-04-11 4 views
0

Ich habe ein paar Blogs darüber gelesen, wie man benutzerdefinierten Befehl an XAM haken kann, aber ich bin immer noch unscharf, wie man es mit meiner eigenen Probe implementieren kann. Es muss etwas geben, was ich entlang der Linie vermisst habe und mir nicht bewusst war.benutzerdefinierter Befehl auf wpf

Ich möchte versuchen, eine Klasse zu erstellen, die meine Befehle enthält, die ich sie wiederverwenden kann, dann versuchen, es mit meiner Schaltfläche auf XAML zu haken.

Das ist mein Befehl Klasse:

namespace TestCommand.Commands 
{ 
    public static class TestButtonCommand 
    { 
     static RoutedUICommand addFruit = 
      new RoutedUICommand("Add new fruit name", "AddFruit", typeof(TestButtonCommand)); 
     public static RoutedUICommand AddFruit 
     { 
      get 
      { 
       return addFruit; 
      } 
     } 
    } 
} 

Meine XAML-Klasse:

<Window x:Class="TestCommand.MainWindow" 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
       xmlns:local="clr-namespace:TestCommand" 
       xmlns:MyCommands='clr-namespace:TestCommand.Commands' 
       mc:Ignorable="d" 
       Title="MainWindow" 
       Height="350" 
       Width="525"> 
    <StackPanel Orientation='Vertical'> 
     <Button x:Name='AddFruit' 
         Height='auto' 
         Width='auto' 
         HorizontalAlignment='Center' 
         Content='Add New Fruit' 
         Margin='0,25,0,10' 
         Click='AddFruit_Click' /> 
     <Button x:Name='AddFruit2' 
         Height='auto' 
         Width='auto' 
         HorizontalAlignment='Center' 
         Content='Add New Fruit 2' 
         Margin='0,10,0,100'> 
      <Button.CommandBindings> 
       <CommandBinding Command='{x:Static MyCommands:TestButtonCommand.AddFruit}' 
               Executed='CommandBinding_Executed' 
               CanExecute='CommandBinding_CanExecute' /> 
      </Button.CommandBindings> 
     </Button> 
    </StackPanel> 
</Window> 

Mein Code hinter

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void AddFruit_Click(object sender, RoutedEventArgs e) 
    { 
     Fruits.Add("Durian", "Green"); 
    } 

    private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e) 
    { 
     Fruits.Add("Durian", "Green"); 
    } 

    private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e) 
    { 
     e.CanExecute = true; 
    } 
} 

}

Aus Gründen der Vergleich, die Der erste Knopf benutzt das Tradi und ich kann sehen, dass das Event gefeuert wurde und neue Früchte hinzugefügt wurden, aber als ich auf den 2. Button klickte, passierte nichts. Ich muss es nicht richtig angehakt haben. Ich würde jeden Hinweis, Zeiger von dem, was falsch gemacht wurde, schätzen.

sind die Modellklassen im Folgenden aufgeführt:

namespace TestCommand.Models 
{ 
    public class Fruit 
    { 
     public string FruitName { get; set; } 
     public string FruitColor { get; set; } 
     public bool Selected { get; set; } 

    } 
} 

namespace TestCommand.Models 
{ 
    public class Fruits 
    { 
     private static List<Fruit> _fruitList; 
     public static void Add(string f, string c) 
     { 
      _fruitList.Add(new Fruit 
      { 
       FruitName = f, 
       FruitColor = c, 
       Selected = false 
      }); 
     } 
     static Fruits() 
     { 
      _fruitList = new List<Fruit>(); 
      _fruitList.Add(new Fruit 
      { 
       FruitName = "Mango", 
       FruitColor = "Yellow", 
       Selected = false 
      }); 
      _fruitList.Add(new Fruit 
      { 
       FruitName = "Mango", 
       FruitColor = "Yellow", 
       Selected = false 
      }); 
      _fruitList.Add(new Fruit 
      { 
       FruitName = "Water Melon", 
       FruitColor = "Green", 
       Selected = false 
      }); 
      _fruitList.Add(new Fruit 
      { 
       FruitName = "Apple", 
       FruitColor = "Red", 
       Selected = false 
      }); 
      _fruitList.Add(new Fruit 
      { 
       FruitName = "Banana", 
       FruitColor = "Yellow", 
       Selected = false 
      }); 
      _fruitList.Add(new Fruit 
      { 
       FruitName = "Orange", 
       FruitColor = "Orange", 
       Selected = false 
      }); 
     } 
     public static List<Fruit> getAllFruit(bool bSelected = false) 
     { 
      var result = (bSelected ? 
             _fruitList.Where(x => x.Selected = true).ToList<Fruit>() 
             : _fruitList.ToList<Fruit>()); 
      return result; 
     } 
    } 
} 

Antwort

2

Versuchen Sie, die Command Eigenschaft des Button auf Ihren Befehl zu setzen:

<Button x:Name='AddFruit2' 
     Height='auto' 
     Width='auto' 
     HorizontalAlignment='Center' 
     Content='Add New Fruit 2' 
     Margin='0,10,0,100' 
     Command="{x:Static MyCommands:TestButtonCommand.AddFruit}"> 
    <Button.CommandBindings> 
     <CommandBinding Command='{x:Static MyCommands:TestButtonCommand.AddFruit}' 
         Executed='CommandBinding_Executed' 
         CanExecute='CommandBinding_CanExecute' /> 
    </Button.CommandBindings> 
</Button> 
Verwandte Themen