嗨,我有一个列表框,每一行包含一个文本框和一个按钮;
使用单击按钮从列表框中删除该行;此操作使用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>在viewmodel类中,我有以下命令:
private ObjectButtonCommand<Category> _deleteCommand;
public ObjectButtonCommand<Category> DeleteCommand
{
get
{
return _deleteCommand
?? (_deleteCommand = new ObjectButtonCommand<Category>(
_category =>
{
GetAllCategories.Remove(_category);
}));
}
}GetAllCategories是观察性的;
这是我的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)参数);
{“无法将类型为'System.Windows.Controls.SelectedItemCollection‘的对象强制转换为’Sepand.WPFProject.Model.Model.类别‘”}
我该怎么办?
发布于 2016-03-27 10:23:18
将所选内容传递给delete命令(它是一个列表),因此不能在这两种情况下使用相同的命令,除非您首先将单个项(从DataTemplate中传递)包装在一个列表中。
您可能应该定义一个新命令,该命令以IList作为参数(ListBox.SelectedItems类型),然后将其项转换为Category并单独删除。
如果您只想删除单个选定的项,则需要更改到SelectedItem的绑定,并需要能够在现有命令中处理SelectedItem是null的情况。例如,如果CanExecute受到InvokeCommandAction的尊重,将其更改为parameter != null。
发布于 2016-03-27 09:32:13
嗨,穆罕默德,你能试试这个吗?
对于您的Listbox,请确保SelectionMode设置为Single。然后更改您在CommandParameter中引用的CommandParameter而不是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>https://stackoverflow.com/questions/36245459
复制相似问题