当我按下按钮时,我正试图弄清楚如何在ListView中选中一行。内部有一个包含多列的GridView。这些列绑定到我创建的类中的字段,这些字段被发送到ObservableCollection,后者填充ListView中的数据。
最好在单击按钮后,整个选定的行会有一条红线,尽管此时我甚至会对每个单元格的文本被击中感到满意。
我尝试过各种各样的事情。我得到的最接近的是,通过将我的预订类中的Notes字段更改为TextBlock,然后添加如下所示的TextDecoration,我能够获得工具提示以显示带有删除的文本:
Reservation selected = (Reservation)shuttleView.SelectedItem;
TextDecoration td = new TextDecoration(TextDecorationLocation.Strikethrough, new Pen(Brushes.Black, 1), 0, TextDecorationUnit.FontRecommended, TextDecorationUnit.FontRecommended);
selected.Notes.TextDecorations.Add(td);
但是,这只会将装饰放在工具提示上,而不是放在ListView中......
我在下面发布了我的列表视图和课程:
<ListView Height="287" HorizontalAlignment="Left" Margin="148,12,0,0" Name="shuttleView" VerticalAlignment="Top" Width="720" >
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="ToolTip" Value="{Binding Notes}" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Width="50" Header="Time"
DisplayMemberBinding="{Binding Time}" />
<GridViewColumn Width="50" Header="DO/PU"
DisplayMemberBinding="{Binding DropPickup}" />
<GridViewColumn Width="100" Header="# People"
DisplayMemberBinding="{Binding People}" />
<GridViewColumn Width="100" Header="Room #"
DisplayMemberBinding="{Binding Room}" />
<GridViewColumn Width="100" Header="Hotel"
DisplayMemberBinding="{Binding Hotel}" />
<GridViewColumn Width="112" Header="Location"
DisplayMemberBinding="{Binding Location}" />
<GridViewColumn Width="198" Header="Notes"
DisplayMemberBinding="{Binding Notes}" />
</GridView>
</ListView.View>
</ListView>
class Reservation
{
public string ResID { get; set; }
public string Time { get; set; }
public string DropPickup { get; set; }
public string People { get; set; }
public string Room { get; set; }
public string Hotel { get; set; }
public string Location { get; set; }
public string Notes { get; set; }
public Reservation(string ResID, string Time, string DropPickup, string People, string Room, string Hotel, string Location, string Notes)
{
this.ResID = ResID;
this.Time = Time;
this.DropPickup = DropPickup;
this.People = People;
this.Room = Room;
this.Hotel = Hotel;
this.Location = Location;
this.Notes = Notes;
}
}
答案 0 :(得分:4)
你可以修改这个ListViewItem ControlTemplate Example并添加一个薄矩形来打击整个列表视图项:
<ListView>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Grid>
<Border Name="Border" Padding="2" SnapsToDevicePixels="true" Background="Transparent">
<GridViewRowPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<Rectangle Name="StrikeThrough" HorizontalAlignment="Stretch" VerticalAlignment="Center"
Height="1" StrokeThickness="1" Stroke="Transparent"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="Background" Value="LightBlue"/>
<Setter TargetName="StrikeThrough" Property="Stroke" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
...
</ListView>
答案 1 :(得分:0)
这是一个适合我的例子。 TextDecoration是数据绑定的,因此它依赖于IValueConverter返回“Strikethrough”。
<Style x:Key="z3r0_Style_ListBoxItem_Default" TargetType="{x:Type ListBoxItem}">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="FontSize" Value="11"/>
<Setter Property="FontFamily" Value="Consolas"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid>
<Border Name="Border" Padding="2" SnapsToDevicePixels="true" Background="Transparent">
</Border>
<TextBlock Name ="_Text" Padding ="3" IsHitTestVisible="false" TextDecorations="None" Text="{Binding}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="_Text" Property="Foreground" Value= "{Binding Converter={StaticResource m_Converter_ListBoxItem_Foreground_Selected}}"/>
<Setter TargetName="_Text" Property="TextDecorations" Value = "{Binding Converter={StaticResource m_Converter_ListBoxItem_TextDecoration}}"/>
<Setter TargetName="Border" Property="Background" Value="{StaticResource z3r0_SolidColorBrush_Green}"/>
</Trigger>
<Trigger Property="IsSelected" Value="false">
<Setter TargetName="_Text" Property="Foreground" Value= "{Binding Converter={StaticResource m_Converter_ListBoxItem_Foreground_Unselected}}"/>
<Setter TargetName="_Text" Property="TextDecorations" Value = "{Binding Converter={StaticResource m_Converter_ListBoxItem_TextDecoration}}"/>
<Setter TargetName="Border" Property="Background" Value="Transparent"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>