首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >结合SelectedItem of ListBox

结合SelectedItem of ListBox
EN

Stack Overflow用户
提问于 2016-03-27 08:42:01
回答 2查看 986关注 0票数 1

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

使用单击按钮从列表框中删除该行;此操作使用mvvm模式。

这是我的命令。

这是我的xaml:

代码语言:javascript
复制
<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>
代码语言:javascript
复制
<ListBox Grid.Column="0"  Grid.Row="0" Name="lstCategory"
         ItemTemplate="{StaticResource CategoryTemplate}"
         ItemsSource="{Binding Path=GetAllCategories}">

</ListBox>

在viewmodel类中,我有以下命令:

代码语言:javascript
复制
private ObjectButtonCommand<Category> _deleteCommand;
public ObjectButtonCommand<Category> DeleteCommand
{
    get
    {
        return _deleteCommand
          ?? (_deleteCommand = new ObjectButtonCommand<Category>(
            _category =>
            {
                GetAllCategories.Remove(_category);

            }));
    }
}

GetAllCategories是观察性的;

这是我的ObjectButtonCommand代码:

代码语言:javascript
复制
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);
    }
}

现在每件事都是好的,当点击按钮那行删除;

现在,当我选择一行列表框时,我希望这个过程重复。

我试着用这个代码:

代码语言:javascript
复制
<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)参数);

{“无法将类型为'System.Windows.Controls.SelectedItemCollection‘的对象强制转换为’Sepand.WPFProject.Model.Model.类别‘”}

我该怎么办?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-03-27 10:23:18

将所选内容传递给delete命令(它是一个列表),因此不能在这两种情况下使用相同的命令,除非您首先将单个项(从DataTemplate中传递)包装在一个列表中。

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

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

票数 1
EN

Stack Overflow用户

发布于 2016-03-27 09:32:13

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

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

代码语言:javascript
复制
                <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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36245459

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档