C#文件:爲什麼下面的WPF命令不會觸發?
public partial class MainWindow : Window
{
public DelegateCommand<ICollection<string>> TestCommand { get; set; }
public ICollection<string> TestParameter
{
get
{
List<string> lstParams = new List<string>() { "test", "test2", "test3" };
return lstParams;
}
}
public MainWindow()
{
InitializeComponent();
TestCommand = new DelegateCommand<ICollection<string>>(TestMethod);
}
private void TestMethod(ICollection<string> param)
{
if (param != null)
{
lblTest.Content = "Hit";
}
}
}
.XAML文件
<Window x:Class="WPFAttachedBehaviorTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:local="clr-namespace:WPFAttachedBehaviorTest"
Title="MainWindow" Height="350" Width="525" DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction CommandParameter="{Binding Path=TestParameter}" Command="{Binding Path=TestCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<Label x:Name="lblTest"></Label>
</Window>
在TestParameter吸氣觸發一個破發點,但TestMethod的永遠不會觸發。
我沒有看到在輸出窗口任何綁定錯誤(相反,如果我故意錯拼TestCommand2它會抱怨 - 所以我想這是正確的)
這是使用棱鏡DelegateCommand和Expression Blend的InvokeCommandAction行爲