0
私はカスタムコマンドをxamにフックする方法に関するいくつかのブログを読んだが、自分のサンプルを使ってカスタムコマンドを実装する方法についてはまだ曖昧だ。私がラインに沿って見逃したことがあり、気づいていないものがなければならない。wpfのカスタムコマンド
私はそれらを再利用できるコマンドを含むクラスを作成して、xamlのボタンにフックしようとします。
これは私のコマンドクラスである:
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;
}
}
}
}
私のXAMLクラス:比較のために
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;
}
}
}
背後
<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>
は私のコード、最初のボタンはトラディを使用しますイベントが発生し、新しい果物が追加されたことがわかりますが、2番目のボタンをクリックしても何も起こりませんでした。私はそれを正しく引っ掛けてはいけません。 私は何かヒント、何が間違って行われたのポインタを感謝します。
マイ・モデル・クラスは次のとおりです。
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;
}
}
}