我试图弄清楚如何在按下按钮时删除 ListView 中选定的行。里面有一个GridView,有多个列。这些列绑定(bind)到我创建的类中的字段,该类被发送到 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 和类:
<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;
}
}
最佳答案
您可以修改此 ListViewItem ControlTemplate Example并添加一个细矩形以贯穿整个 ListView 项:
<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>
关于wpf - 单击按钮时向 ListView 中的选定行添加删除线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10264483/