简体   繁体   English

WPF Prism列表框按钮

[英]WPF Prism listbox button

I am pulling items from DB into a listbox which consists of several columns. 我将项目从数据库拉到一个包含几列的列表框中。 I would like to add a delete button to each item I pull from DB, but I can't seem to forward the ID of the item, it always says 0. 我想为从数据库中提取的每个项目添加一个删除按钮,但是我似乎无法转发该项目的ID,它始终显示为0。

<ListBox ItemsSource="{Binding LbPlugins}" HorizontalContentAlignment="Stretch" Grid.Row="1">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid HorizontalAlignment="Stretch">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="5*"/>
                    <ColumnDefinition Width="5*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>

                <CheckBox Grid.Column="0" Content="{Binding Name}" IsChecked="{Binding IsActive}"/>
                <Label Grid.Column="1" Content="{Binding ClassName}"/>
                <Button Grid.Column="2" Content="E" Command="{Binding btnEditPluginCommand}"/>
                <Button Grid.Column="3" Content="D" Command="{Binding Path=DataContext.btnDeletePluginCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}" CommandParameter="{Binding PluginId}"/>

            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

In ViewModel: 在ViewModel中:

    private int pluginId;
    public int PluginId
    {
        get { return pluginId; }
        set { SetProperty(ref pluginId, value); }
    }
    public DelegateCommand btnDeletePluginCommand { get; set; }

... ...

in constructor 在构造函数中

btnDeletePluginCommand = new DelegateCommand(DeletePlugin);

... ...

private void DeletePlugin()
{
    var result = MessageBox.Show("Are you sure you want to delete this plugin?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
    if (result == DialogResult.Yes)
    {
        MessageBox.Show("YAY, ID=" + pluginId);
    }
}

As you're using Prism you should use DelegateCommand , that is Prism's ICommand implementation. 使用Prism时,应使用DelegateCommand ,这是Prism的ICommand实现。

In order to make this work you need to use an object or a nullable type as argument for the generic CommandDelegate . 为了使这项工作有效,您需要使用一个对象或可为null的类型作为通用CommandDelegate参数。 If you fail to do this, you'll get an InvalidCastException at runtime. 如果不这样做,则在运行时将收到InvalidCastException

Declare your command like this: 像这样声明您的命令:

public ICommand btnDeletePluginCommand { get; set; }

Initialize it on viewmodel's constructor: 在viewmodel的构造函数上初始化它:

btnDeletePluginCommand = new DelegateCommand<int?>(DeletePlugin);

And refactor your method: 并重构您的方法:

private void DeletePlugin(int? pluginId)
{
    if (pluginId == null) return;

    var result = MessageBox.Show("Are you sure you want to delete this plugin?", "", MessageBoxButtons.YesNo,
        MessageBoxIcon.Warning);
    if (result == DialogResult.Yes)
        MessageBox.Show("YAY, ID=" + pluginId);
}

As you did bind the command to the ListBox, pluginId parameter will never be null, but you should always validate. 当您确实将命令绑定到ListBox时,pluginId参数永远不会为null,但您应该始终进行验证。 Maybe you use this viewmodel with other UI components? 也许您将此ViewModel与其他UI组件一起使用?

By the way, you shouldn't be using MessageBox in the viewmodel. 顺便说一句,您不应该在视图模型中使用MessageBox I guess it's a proof of concept or something :). 我想这是概念的证明或:)。 In the case of MVVM you should inject a DialogService or use InteractionRequests to show notifications from your viewmodel. 对于MVVM,您应该注入DialogService或使用InteractionRequests显示来自视图模型的通知。

Hope this helps! 希望这可以帮助!

Since you already passing the parameter from xaml, you can use: 由于您已经从xaml传递了参数,因此可以使用:

btnDeletePluginCommand = new Microsoft.Practices.Prism.Commands.DelegateCommand<int>(DeletePlugin);

And then your DeletePlugin should have one parameter like this: 然后,您的DeletePlugin应该具有这样的一个参数:

private void DeletePlugin(int pluginId)
{
    ...
}

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

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