我正在尝试使用 VS2008 和 .NET 框架的 3.5 以编程方式突出显示 WPF listview 控件中的第一行。这是用于此的 C# 代码:
ListViewItem Val = (ListViewItem)ListView1.Items[0];
Val.IsSelected = true;
代码在第一行引发异常,这是在 ListView1 填充数据之后。异常中的消息说:
“无法将‘Message.LV1Data’类型的对象转换为‘System.Windows.Controls.ListViewItem’类型。”
LV1Data 是我用来绑定此控件中的列的类。因此,它看起来像是在尝试返回 LV1Data 对象而不是 ListViewItem 对象。有没有人对我做错了什么或我需要做什么以以编程方式突出显示列表视图行有任何建议?
下面是 ListView 控件的 XAML 代码:
<ListView x:Name="ListView1" ItemContainerStyle="{StaticResource alternatingListViewItemStyle}" AlternationCount="2" SelectionChanged="ListView1_SelectionChanged"
SelectionMode="Multiple" HorizontalAlignment="Left" ItemsSource = "{Binding ElementName=LobbyWindow, Path=ListCollection1}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Game}">
<GridViewColumnHeader Content="Game" FontWeight="Bold" />
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Stakes}">
<GridViewColumnHeader Content="Stakes" Width="68" FontWeight="Bold" />
</GridViewColumn>
<GridViewColumn Width="30" DisplayMemberBinding="{Binding Seats}">
<GridViewColumnHeader Content="Seats" FontWeight="Bold" />
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
编辑
<!-- Define the resource for the alternating background background used in the ListView objects. -->
<StackPanel.Resources>
<Style x:Key="alternatingListViewItemStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="IsSelected" Value="{Binding IsSelected}"/>
<Style.Resources>
<!-- Foreground for Selected ListViewItem -->
<!-- <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/> -->
<!-- Background for Selected ListViewItem -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Green"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Brown"/>
</Style.Resources>
<Style.Triggers>
<!-- setting up triggers for alternate background colors -->
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="#FFD9F2BF"></Setter>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="2">
<Setter Property="Background" Value="White"></Setter>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="LightBlue"></Setter>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="LightBlue" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="True"></Condition>
<Condition Property="ItemsControl.AlternationIndex" Value="0"></Condition>
</MultiTrigger.Conditions>
<Setter Property="Background" Value="LightBlue"></Setter>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="True"></Condition>
<Condition Property="ItemsControl.AlternationIndex" Value="1"></Condition>
</MultiTrigger.Conditions>
<Setter Property="Background" Value="LightGreen"></Setter>
</MultiTrigger>
</Style.Triggers>
<!-- setting row height here -->
</Style>
</StackPanel.Resources>