C# 文件:
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 getter 上的断点触发,但 TestMethod 永远不会触发。
我在输出窗口中看不到任何绑定错误(相反,如果我故意拼错 TestCommand2 它会抱怨 - 所以我想这是正确的)
这是使用 Prism DelegateCommand 和 Expression Blend InvokeCommandAction 行为