绑定ListBox的SelectedItem

Binding SelectedItem of ListBox

你好,我有一个列表框,每一行包含一个文本框和一个按钮;

单击按钮从列表框中删除该行;这适用于 mvvm 模式

我为此使用命令。

这是我的 xaml:

<DataTemplate x:Key="CategoryTemplate">
    <Border Width="400" Margin="5" BorderThickness="1" BorderBrush="SteelBlue" CornerRadius="4">

        <StackPanel Grid.Row="0" Orientation="Horizontal">
            <TextBlock Width="300" Margin="5" Text="{Binding Path=Name}"></TextBlock>
            <Button Name="btnDeleteCategory" Width="50" Margin="5"   
              Command="{Binding DataContext.DeleteCommand, RelativeSource={RelativeSource AncestorType=ListBox}}"
                CommandParameter="{Binding}" Content="-"   />
        </StackPanel>

    </Border>
</DataTemplate>
<ListBox Grid.Column="0"  Grid.Row="0" Name="lstCategory"
         ItemTemplate="{StaticResource CategoryTemplate}"
         ItemsSource="{Binding Path=GetAllCategories}">

</ListBox>

在视图模型中 class 我有这个命令:

private ObjectButtonCommand<Category> _deleteCommand;
public ObjectButtonCommand<Category> DeleteCommand
{
    get
    {
        return _deleteCommand
          ?? (_deleteCommand = new ObjectButtonCommand<Category>(
            _category =>
            {
                GetAllCategories.Remove(_category);

            }));
    }
}

GetAllCategories 是 observecollection 属性;

这是我的 ObjectButtonCommand 代码:

public class ObjectButtonCommand<T> : ICommand
    where T:class
{
    private Action<T> WhatToExecute;

    public ObjectButtonCommand(Action<T> What)
    {
        WhatToExecute = What;
    }

    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public void Execute(object parameter)
    {
        WhatToExecute((T)parameter);
    }
}

现在一切正常,当单击该行删除按钮时;

现在我希望当我 select 一行列表框

时重复此过程

我试试这个代码:

<ListBox Grid.Column="0"  Grid.Row="0" Name="lstCategory" ItemTemplate="{StaticResource CategoryTemplate}" ItemsSource="{Binding Path=GetAllCategories}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <i:InvokeCommandAction  Command="{Binding DataContext.DeleteCommand , RelativeSource={RelativeSource AncestorType=ListBox}}" CommandParameter="{Binding Path=SelectedItems,ElementName=lstCategory}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ListBox>

但我在这段代码中收到此错误:WhatToExecute((T)parameter);

{"Unable to cast object of type 'System.Windows.Controls.SelectedItemCollection' to type 'Sepand.WPFProject.Model.Model.Category'."}

我该怎么办?

嗨,穆罕默德,你能试试这个吗:

对于您的 Listbox,确保您的 SelectionMode 设置为 Single。然后更改您在 CommandParameter 中引用 SelectedItem 而不是 SelectedItems。像这样:

                <ListBox Grid.Column="0"  Grid.Row="0" Name="lstCategory" ItemTemplate="{StaticResource CategoryTemplate}" ItemsSource="{Binding Path=GetAllCategories}" SelectionMode="Single">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SelectionChanged">
                        <i:InvokeCommandAction  Command="{Binding DataContext.DeleteCommand , RelativeSource={RelativeSource AncestorType=ListBox}}" CommandParameter="{Binding Path=SelectedItem,ElementName=lstCategory}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>

            </ListBox>

您将选择传递给作为列表的删除命令,因此您不能对这两种情况使用相同的命令,除非您首先将单个项目(从 DataTemplate 传递)包装在列表中.

您可能应该定义一个新命令,该命令将 IList 作为参数(ListBox.SelectedItems 类型),然后将其项目转换为 Category 并单独删除。

如果您只想删除单个选定项目,则需要将绑定更改为 SelectedItem,并且需要能够处理 SelectedItem 在您现有的 null 中的情况命令。例如将 CanExecute 更改为 parameter != null 如果 InvokeCommandAction.

遵守