在WPF中使用ICommand重写“button.Click + = ...”

问题描述 投票:0回答:1

我正在使用外部SDK。

namespace ProSimSDK 
{
    public class ArmedFailure
   {
     ...
     public static event ArmedFailureEventDelegate onNew;
     public void Reset();
     ...
   }
}

namespace ProSimSDK
{
   public delegate void ArmedFailureEventDelegate(ArmedFailure armedFailure);
}

当我尝试通过WPF重写一些Winform代码时遇到了一些麻烦。在Winform中:

public Form1()
{
    InitializeComponent();
    ArmedFailure.onNew += new ArmedFailureEventDelegate(ArmedFailure_onNew);
}

// This function will be called when a new armedFailure is received
void ArmedFailure_onNew(ArmedFailure armedFailure)
{
      //Here is the code I need to rewrite in WPF.
      removeButton.Click += new EventHandler(delegate(object sender, EventArgs e)
        {
            failure.Reset();
        });
}

在WPF中,我使用列表框。有了一些指南,我正在使用ListBox模板和命令。在Window1.xaml中:

<DataTemplate x:Key="ListBoxItemTemplate">
    <Grid>
        <TextBlock x:Name="TB" Margin="5,10,5,5" Grid.Column="2" Height="23" Text="{Binding}" VerticalAlignment="Top" />
        <Button HorizontalAlignment="Right" Grid.Column="3" Margin="500,10,5,0" CommandParameter="{Binding}" Command="{Binding ElementName=UC_Failures_Setting, Path=OnClickCommand}" Width="80" Click="Button_Click">remove</Button>
    </Grid>
</DataTemplate>

<ListBox x:Name="listbox" ItemTemplate="{StaticResource ListBoxItemTemplate}" Margin="0,661,982,0" SelectionChanged="ListBox_SelectionChanged">

Window1.xaml.cs

public Window1()
{
    InitializeComponent();

    //How to implement the same functionality of "removeButton.Click += new EventHandler(delegate(object sender, EventArgs e) {failure.Reset();});" shown in Winform???
    OnClickCommand = new ActionCommand(x => listbox.Items.Remove(x));
}

ActionCommand.cs:

 public class ActionCommand: ICommand
 {
    private readonly Action<object> Action;
    private readonly Predicate<object> Predicate;

    public ActionCommand(Action<object> action) : this(action, x => true)
    {

    }
    public ActionCommand(Action<object> action, Predicate<object> predicate)
    {
        Action = action;
        Predicate = predicate;
    }

    public bool CanExecute(object parameter)
    {
        return Predicate(parameter);
    }
    public void Execute(object parameter)
    {
        Action(parameter);
    }

    public event EventHandler CanExecuteChanged
    {
        add
        {
            CommandManager.RequerySuggested += value;
        }
        remove
        {
            CommandManager.RequerySuggested -= value;
        }
    }
}

我的列表框中的按钮如何实现相同的功能

removeButton.Click += new EventHandler(delegate(object sender, EventArgs e) 
{ failure.Reset(); });

在Winform中显示?在WPF中,我不能这样写。谢谢。

c# wpf winforms icommand
1个回答
1
投票

如果ListBox填充了ArmedFailure项目,那么命令收到的参数应该是ArmedFailure项目。

OnClickCommand = new ActionCommand
( 
   x => 
   {
       var failure = (ArmedFailure)x;
       failure.Reset();
       listbox.Items.Remove(x);
   }
);

WinForms中的Button.Click处理程序中的所有内容都成为wpf中ICommand.Execute的一部分

© www.soinside.com 2019 - 2024. All rights reserved.