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類:
<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>
我的代碼背後
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;
}
}
}
爲了比較起見,第一個按鈕使用傳統我可以看到事件被解僱並添加了新的水果,但當我點擊第二個按鈕時,什麼都沒有發生。我一定沒有正確地掛鉤它。 我將不勝感激任何提示,指示什麼是不正確的。
我的模型類列舉如下:
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;
}
}
}