【问题标题】:Binding SelectedItem of ListBox绑定 ListBox 的 SelectedItem
【发布时间】:2016-03-27 08:42:01
【问题描述】:

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

使用单击按钮从列表框中删除该行;这适用于 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>

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

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);
    }
}

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

现在我希望当我选择一行列表框时重复此过程

我试试这个代码:

<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);

{“无法将'System.Windows.Controls.SelectedItemCollection'类型的对象转换为'Sepand.WPFProject.Model.Model.Category'类型。”}

我该怎么办?

【问题讨论】:

  • _deleteCommand 应该是 readonly。另外,下次请正确格式化您的问题。

标签: c# wpf mvvm binding selecteditem


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    嗨,Mohammad,你能试试这个吗:

    对于您的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>
    

    【讨论】:

    • 现在我收到此错误 {"Value cannot be null.\r\nParameter name: entity"}
    • 仔细查看堆栈跟踪和行号。回到你的代码并尝试确定什么是空的。您可能正在对已删除的对象运行 LINQ。但是如果没有所有代码和完整的堆栈跟踪,就不可能在这里进行故障排除。随意更改您的问题以包含更多代码并尽可能清晰,更多人会尝试并提供帮助。
    猜你喜欢
    • 1970-01-01
    • 2012-08-26
    • 2010-12-26
    • 2011-01-02
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多