You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C# WPF中通过IsSelected属性程序化预选中ListView项?

WPF ListView 实现列表项预选中的正确方法

问题场景

开发C# WPF应用时,ListView功能正常,需要实现列表项的预选中(选中状态对用户可见且支持用户修改)。最初尝试让Person类继承ListViewItem来设置IsSelected,虽然能看到选中行,但行内容为空,需要正确的实现方式。

原XAML代码:

<Grid>
    <ListView Name="lvEntries" SelectionMode="Multiple" Margin="0,0,0,116">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="100" Header="Lastname" DisplayMemberBinding="{Binding LastName}"/>
                <GridViewColumn Width="100" Header="Firstname" DisplayMemberBinding="{Binding FirstName}"/>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

原Person类:

public class Person
{
    public string LastName { get; set; }
    public string FirstName { get; set; }
}

解决方案一:直接操作ListView的SelectedItems集合

不需要修改Person类,在后台代码添加完数据后,直接将需要预选中的项加入SelectedItems集合即可:

修改后的后台代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        lvEntries.Items.Clear();
        
        Person newPerson = new Person();
        newPerson.FirstName = "Jack";
        newPerson.LastName = "Nicholson";
        lvEntries.Items.Add(newPerson);
        
        Person newPerson2 = new Person();
        newPerson2.FirstName = "Bill";
        newPerson2.LastName = "Murray";
        lvEntries.Items.Add(newPerson2);
        
        // 预选中指定项
        lvEntries.SelectedItems.Add(newPerson);
        lvEntries.SelectedItems.Add(newPerson2);
    }
}

注意:如果是单选模式(SelectionMode="Single"),直接设置lvEntries.SelectedItem = newPerson;即可。

解决方案二:数据绑定选中状态(推荐,符合WPF设计思想)

这种方式将选中状态与数据模型绑定,适配复杂场景和MVVM模式,同时支持双向联动(用户修改选中状态时,数据模型的属性会同步更新):

  1. 修改Person类,添加IsSelected属性并实现INotifyPropertyChanged接口:
public class Person : INotifyPropertyChanged
{
    private string _lastName;
    public string LastName
    {
        get => _lastName;
        set
        {
            _lastName = value;
            OnPropertyChanged(nameof(LastName));
        }
    }

    private string _firstName;
    public string FirstName
    {
        get => _firstName;
        set
        {
            _firstName = value;
            OnPropertyChanged(nameof(FirstName));
        }
    }

    private bool _isSelected;
    public bool IsSelected
    {
        get => _isSelected;
        set
        {
            _isSelected = value;
            OnPropertyChanged(nameof(IsSelected));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
  1. 修改XAML,为ListView的ItemContainerStyle绑定IsSelected属性:
<Grid>
    <ListView Name="lvEntries" SelectionMode="Multiple" Margin="0,0,0,116">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.View>
            <GridView>
                <GridViewColumn Width="100" Header="Lastname" DisplayMemberBinding="{Binding LastName}"/>
                <GridViewColumn Width="100" Header="Firstname" DisplayMemberBinding="{Binding FirstName}"/>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>
  1. 后台代码中直接设置Person的IsSelected属性:
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        lvEntries.Items.Clear();
        
        Person newPerson = new Person();
        newPerson.FirstName = "Jack";
        newPerson.LastName = "Nicholson";
        newPerson.IsSelected = true; // 标记为预选中
        lvEntries.Items.Add(newPerson);
        
        Person newPerson2 = new Person();
        newPerson2.FirstName = "Bill";
        newPerson2.LastName = "Murray";
        newPerson2.IsSelected = true; // 标记为预选中
        lvEntries.Items.Add(newPerson2);
    }
}

内容的提问来源于stack exchange,提问作者Emil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 19:55:00