繁体   English   中英

WPF按钮动态命令

[英]WPF Button Dynamic Command

我有一个WPF表格,如下所示;

<ListBox x:Name="lbModules" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Button Command="{Binding OnClick}">
                <StackPanel>
                    <Image Source="{Binding ModuleIcon}"/>
                    <Label Content="{Binding ModuleName}"/>
                </StackPanel>
            </Button>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel></WrapPanel>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

在后面的代码中, lbModules被赋予List<ModuleButton>作为ItemsSource,其中ModuleButton定义如下;

internal class ModuleButton
{
    public ImageSource ModuleIcon {get; set;}
    public string ModuleName {get; set;}
    public ICommand OnClick {get; set;}
}

我的问题是以动态方式定义OnClick命令。 我需要这样做,因为我正在使用MEF,而OnClick事件在技术上是在不同的程序集中。 我只需要调用module.GetForm()但它似乎并不那么简单......

我按如下方式构建ModuleButton(s);

Lazy<IModule, IModuleMetadata> moduleCopy = module;
ModuleButton button = new ModuleButton
{
    ModuleName = moduleCopy.Metadata.ModuleName,
    ModuleIcon = 
        Imaging.CreateBitmapSourceFromHBitmap(moduleCopy.Value.ModuleButtonIcon.GetHbitmap(),
            IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()),
    OnClick = // TODO: Somehow call moduleCopy.Value.GetForm()
};

我一直在广泛搜索,检查谷歌的各种结果, 是我最新的消息来源之一。

有可能做我想做的事吗? 如果是这样,怎么样?

好的,试试我的版本,因为Patrick的回答没有实现ICommand's CanExecuteChanged event所以你不能顺利编译; 此外,这个RelayCommand已经重载'ctor只接受一个参数CanExecute总是返回true - 使它更容易使用。

它取自MSDN杂志文章WPF Apps with Model-View-ViewModel Design Pattern

public class RelayCommand : ICommand
{
    #region Fields

    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;

    #endregion // Fields

    #region Constructors

    /// <summary>
    /// Creates a new command that can always execute.
    /// </summary>
    /// <param name="execute">The execution logic.</param>
    public RelayCommand(Action<object> execute)
        : this(execute, null)
    {
    }

    /// <summary>
    /// Creates a new command.
    /// </summary>
    /// <param name="execute">The execution logic.</param>
    /// <param name="canExecute">The execution status logic.</param>
    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");

        _execute = execute;
        _canExecute = canExecute;
    }

    #endregion // Constructors

    #region ICommand Members

    [DebuggerStepThrough]
    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }

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

    public void Execute(object parameter)
    {
        _execute(parameter);
    }

    #endregion // ICommand Members
}

OnClick = new RelayCommand ((o) => 
    {
        moduleCopy.Value.GetForm();
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM