简体   繁体   English

listview mvvm中的WPF删除按钮

[英]WPF Delete button in listview mvvm

I have a button inside a listview to delete selected item.when i click on the button RemoveSubjectCommand is not firing. 我在listview中有一个按钮来删除所选项目。当我点击按钮RemoveSubjectCommand没有触发时。 if i put the button outside the listview it is working fine. 如果我把按钮放在列表视图之外它工作正常。 hopw this is just because of nested item. 这只是因为嵌套项目。 how can i solve this problem? 我怎么解决这个问题?

<ListView HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
        HorizontalContentAlignment="Stretch" Grid.ColumnSpan="3" Grid.Row="2"
        ItemsSource="{Binding AssignedSubjects}">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Center" />
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.View>
        <GridView>
            <GridViewColumn Width="140" Header="Subjects" DisplayMemberBinding="{Binding Name}" />
            <GridViewColumn Width="auto">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="X" Command="{Binding RemoveSubjectCommand}"  />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

View Model, 查看模型,

private ICommand removeSubjectCommand;
**
public ICommand RemoveSubjectCommand
{
    get { return removeSubjectCommand ?? (removeSubjectCommand = new RelayCommand(param => this.RemoveSubject(), null)); }
}
**
private void RemoveSubject()
{ ***
}

If i put following code, it will work fine. 如果我下面的代码,它将正常工作。

<ListView.InputBindings>
    <KeyBinding Key="Delete" Command="{Binding RemoveSubjectCommand}" />
</ListView.InputBindings>

That is because DataContext of button is ListBoxItem DataContext. 那是因为按钮的DataContext是ListBoxItem DataContext。 So you need to go to parent ListView DataContext. 所以你需要转到父ListView DataContext。

one way to do that, is to give ListView a name, and to bind with element name 一种方法是给ListView一个名字,并用元素名称绑定

<ListView Name="lv" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
        HorizontalContentAlignment="Stretch" Grid.ColumnSpan="3" Grid.Row="2"
        ItemsSource="{Binding AssignedSubjects}">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Center" />
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.View>
        <GridView>
            <GridViewColumn Width="140" Header="Subjects" DisplayMemberBinding="{Binding Name}" />
            <GridViewColumn Width="auto">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="X" Command="{Binding ElementName=lv,Path=DataContext.RemoveSubjectCommand}"  />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

You need to bind the Command to using FindAncestor . 您需要将Command绑定到使用FindAncestor Otherwise, it'll try to bind to a RemoveSubjectCommand using the ListViewItem 's datacontext, not the ViewModel's. 否则,它将尝试使用ListViewItem的datacontext而不是ViewModel绑定到RemoveSubjectCommand

Try this: 尝试这个:

<Button Content="X" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}, Path=DataContext.RemoveSubjectCommand}"  />

if i put the button outside the listview it is working fine 如果我把按钮放在列表视图之外它工作正常

Because RemoveSubjectCommand property is defined in the data context of ListView , not in the data context of item . 因为RemoveSubjectCommand属性是在ListView的数据上下文中定义的,而不是在item的数据上下文中。

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

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