当property=true时,将ListviewItem设置为选中
本文关键字:设置 ListviewItem property true | 更新日期: 2023-09-27 18:00:12
我有一个Listview。
XAML:
<ListView Name="lv_records" SelectionMode="Multiple" SelectionChanged="lv_records_SelectionChanged" />
我用一个简单的类将这个Listview的Itemsource设置为List。
类别:
public class Container
{
public string Database { get; set; }
public string Value { get; set; }
public bool Selected { get; set; }
public Container(string db, string val)
{
Database = db;
Value = val;
Selected = false;
}
}
和
lv_records.ItemsSource = ContainerList
但是我想根据布尔值来选择元素。这怎么可能?提前谢谢。
在ItemContainerStyle
:中绑定ListViewItem的IsSelected
属性
<ListView ...>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="IsSelected" Value="{Binding Selected}"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
为了通知Selected
属性的更改,Container类必须实现INotifyPropertyChanged
接口:
public class Container : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool selected;
public bool Selected
{
get { return selected; }
set
{
selected = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Selected"));
}
}
...
}