wpf mvvm 实现listview默认选中首行并高亮
时间: 2023-08-25 20:04:35 浏览: 59
要实现WPF MVVM模式下ListView默认选中首行并高亮,可以使用以下方法:
1. 在ViewModel中定义一个SelectedItem属性,用于绑定ListView的SelectedItems属性。
2. 在ListView的XAML中,使用以下代码将SelectedItem属性与ViewModel中的属性进行绑定:
```xaml
<ListView ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}">
```
3. 在ListView的ItemContainerStyle中,使用以下代码将首行高亮:
```xaml
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="LightBlue" />
<Setter Property="Foreground" Value="White" />
</Trigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
```
这样,ListView就会默认选中首行并高亮了。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)