wpf - 单击按钮时向 ListView 中的选定行添加删除线

标签 wpf listview gridview text-decorations

我试图弄清楚如何在按下按钮时删除 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/

相关文章:

wpf - MahApps.Metro:禁用窗口动画

WPF-是否可以将 OnVerticalOffsetChanged 事件添加到自定义文本框?

android: ListView 中的 ListView

java - 自定义 GridView

c# - 使用 ASP.NET 网格显示分层 xml 数据

c# - 我可以使用不同的 DataTrigger 绑定(bind)重用样式吗?

wpf - 使用 MEFedMVVM 关闭窗口的最佳方法

java - 获取ListView高度

android - Libgdx 中的 ListView

c# - 使用 Gridview 进行参数化向下钻取