我有一个 C# WPF 应用程序,使用 ListView 基本上可以正常工作。 我的问题是: 我想在列表视图中预先选择项目。选择应该对用户可见,并且用户应该能够修改选择。
我尝试使用一个非常简单的应用程序进行原型设计。
这是我的 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>
这是我的隐藏代码:
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);
}
}
public class Person
{
public string LastName { get; set; }
public string FirstName { get; set; }
}
到目前为止一切都很好。 现在我想做一个预选。 ListViewItem 类有一个 .IsSelected 属性,理论上可以在代码隐藏中设置。 但是
lvEntries.Items[0].IsSelected = true;
或
newPerson.IsSelected = true;
不可用。
然后我尝试从 ListViewItem 派生类“Person” - 现在 .IsSelected 可用。 现在 C# 代码如下所示:
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);
}
}
public class Person : ListViewItem
{
public string LastName { get; set; }
public string FirstName { get; set; }
}
结果很有趣: 启动应用程序后我可以看到所选的行。 但这些行没有内容;-) 它们是空的......
我认为我犯了一些基本上错误的事情。
是否有一种简单的方法可以使用 C# 代码(代码隐藏)在 ListView 中进行选择?
提前非常感谢您! 埃米尔
我会将
IsSelected
添加到项目本身。而且由于您似乎还想更改选择的项目,因此您还应该使其可观察。
在此示例代码中,我使用 CommunityToolkit.Mvvm NuGet 包并用它创建 ViewModel。
PersonViewModel.cs
using CommunityToolkit.Mvvm.ComponentModel;
namespace WpfListViewExample;
// The CommunityToolkit's source generators will create
// UI-interactive properties "FirstName", "LastName" and "IsSelected" for you.
// Don't forget to make this class 'partial'.
public partial class PersonViewModel : ObservableObject
{
[ObservableProperty]
private string _firstName = string.Empty;
[ObservableProperty]
private string _lastName = string.Empty;
[ObservableProperty]
private bool _isSelected;
}
MainViewModel.cs
using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;
namespace WpfListViewExample;
public class MainViewModel : ObservableObject
{
public MainViewModel()
{
// The Smiths are selected by default.
People.Add(new PersonViewModel { FirstName = "John", LastName = "Doe" });
People.Add(new PersonViewModel { FirstName = "Jane", LastName = "Doe" });
People.Add(
new PersonViewModel
{
FirstName = "John",
LastName = "Smith",
IsSelected = true,
});
People.Add(
new PersonViewModel
{
FirstName = "Jane",
LastName = "Smith",
IsSelected = true,
});
People.Add(new PersonViewModel { FirstName = "John", LastName = "Jones" });
People.Add(new PersonViewModel { FirstName = "Jane", LastName = "Jones" });
People.Add(new PersonViewModel { FirstName = "John", LastName = "Williams" });
People.Add(new PersonViewModel { FirstName = "Jane", LastName = "Williams" });
}
public ObservableCollection<PersonViewModel> People { get; } = new();
}
MainWindow.xaml
<Window
x:Class="WpfListViewExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfListViewExample"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="800"
Height="450"
Topmost="True"
mc:Ignorable="d">
<Window.DataContext>
<local:MainViewModel />
</Window.DataContext>
<Grid>
<ListView
ItemsSource="{Binding People}"
SelectionMode="Multiple">
<ListView.View>
<GridView>
<GridViewColumn
Width="100"
DisplayMemberBinding="{Binding LastName}"
Header="Lastname" />
<GridViewColumn
Width="100"
DisplayMemberBinding="{Binding FirstName}"
Header="Firstname" />
</GridView>
</ListView.View>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
</Grid>
</Window>
非常感谢! 非常有趣的解决方案,我会及时尝试...... 与此同时,我使用 ListView 的 .SelectedItems 属性解决了这个问题。 我只需使用 ...SelectedItems.Add(..) 方法来添加我想要选择的项目。 我使用全局变量使 ViewModel 类可以访问 MainWindow。 尽管如此,我认为使用“IsSelected”属性会更优雅,但它与...SelectedItems.Add 一起工作得很好,而且努力还可以。 谢谢大家!