Adding Strike-Through to a selected row in a ListView when a button is clicked

I am trying to figure out how to hit the selected row in my ListView when a button is clicked. There is a gridview inside with multiple columns. These columns are bound to fields in the class I created, which is sent to the ObservableCollection, which fills the data in my ListView.

Preferably, after clicking the button, the entire selected line will have a red line, although it is, although at that moment I would even be pleased that every cell text was hit.

I tried all kinds of things. The closest I got is that I was able to get a tooltip to show the text with a hit by changing the Notes field in my reservation class to TextBlock, and then adding TextDecoration as follows:

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);

However, this would only put the decoration in a tooltip, and not in a ListView ...

I have posted my list and class below:

<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;
    }
}
+3
source share
2 answers

You can modify this ListViewItem ControlTemplate Example and add a thin rectangle to break down the entire list item:

<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>
+4
source

Here is an example that works for me. TextDecoration is data bound, so it depends on how the IValueConverter returns 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>

enter image description here

0
source

All Articles