我使用listview显示根据对象属性自定义背景的可观察对象列表的状态。 我设法使用触发器自定义listview项目的背景,但是一旦我选择了一个项目或将光标移动到其中它就消失了。 我想要做的是在选择项目后更改颜色,但它应该依赖于属性值,就像背景是触发器一样。 这是我目前的状态:
<Grid>
<ListView Name="lvDataBinding" ItemsSource="{Binding List}" HorizontalAlignment="Stretch">
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Stato}" Value="Caricato">
<Setter Property="Background" Value="#FF82CA9D" />
</DataTrigger>
<DataTrigger Binding="{Binding Stato}" Value="Errore">
<Setter Property="Background" Value="#FFF7977A" />
</DataTrigger>
<DataTrigger Binding="{Binding Stato}" Value="InCoda">
<Setter Property="Background" Value="#FFF79A" />
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch" Width="Auto">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<WrapPanel Grid.Column="0" Grid.Row="0" Margin="3">
<TextBlock Text="Nome: " />
<TextBlock Text="{Binding Name}" FontWeight="Bold" />
</WrapPanel>
<WrapPanel Grid.Column="1" Grid.Row="0" Margin="3">
<TextBlock Text="ID tessera: " />
<TextBlock Text="{Binding ID}" FontWeight="Bold" />
</WrapPanel>
<WrapPanel Grid.Column="2" Grid.Row="0" Margin="3" HorizontalAlignment="Right">
<TextBlock Text="Stato: " />
<TextBlock Text="{Binding StatoString}" FontWeight="Bold" />
</WrapPanel>
<WrapPanel Grid.Column="0" Grid.Row="1" Margin="3">
<TextBlock Text="Data: " />
<TextBlock Text="{Binding Time}" FontWeight="Bold" />
</WrapPanel>
<WrapPanel Grid.Column="1" Grid.Row="1" Margin="3">
<TextBlock Text="N. Documento: " />
<TextBlock Text="{Binding Number}" FontWeight="Bold" />
</WrapPanel>
<WrapPanel Grid.Column="2" Grid.Row="1" HorizontalAlignment="Right" Margin="3">
<TextBlock Text="Cassa: " />
<TextBlock Text="{Binding Position}" FontWeight="Bold" />
</WrapPanel>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>