Search code examples
c#wpfxamllistviewtextblock

Listview fore color will be unchanged in selection mode


I have the below XAML

however I want when the user box clicked on any of the textblock in he list view I want the forecolor of the text to be unchanged (currently changed to white) instead I want the border color to be changed how can I do that?

ListView x:Name="LV" ItemsSource= "{Binding  Lggv}"     SelectionChanged="dataGridData_SelectionChanged" >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel >
                            <Border BorderThickness="1" BorderBrush="Black">
                                <TextBlock Text="{Binding Info }" AllowDrop="True"  >
                                    <TextBlock.Background>
                                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">                                              
                                            <GradientStop Color="#FFCEE6C6" Offset="0.008"/>
                                            <GradientStop Color="#FF9ECF8C" Offset="0.987"/>
                                        </LinearGradientBrush>
                                    </TextBlock.Background>



                                </TextBlock>


                            </Border>
                            <Grid >
                                <Grid.Background>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="#FFC5DDFF" Offset="0"/>
                                        <GradientStop Color="#FFA8C8F7" Offset="1"/>
                                    </LinearGradientBrush>
                                </Grid.Background>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="1*"></ColumnDefinition>
                                    <ColumnDefinition Width="1*"></ColumnDefinition>
                                    <ColumnDefinition Width="1*"></ColumnDefinition>
                                </Grid.ColumnDefinitions>
                                <Border Grid.Column="0" BorderThickness="1" BorderBrush="Black">
                                    <TextBlock Text="{Binding DateTime}" ></TextBlock>
                                </Border>
                                <Border Grid.Column="1" BorderThickness="1" BorderBrush="Black">
                                    <TextBlock Text="{Binding ComPort}"></TextBlock>
                                </Border>
                                <Border Grid.Column="2" BorderThickness="1" BorderBrush="Black">
                                    <TextBlock Text="{Binding Data}" ></TextBlock>
                                </Border>
                            </Grid>
                        </StackPanel>
                    </DataTemplate>    


                </ListView.ItemTemplate>


            </ListView>

Solution

  • You should distinguish between the item content and the item container. Handling item selection style should be the responsibility of the container. The following style defines a replacement for the visual representation of a ListViewItem with some basic properties. Depending on your desired visual, you may want to define a different style.

    Define the style as resource:

    <Style x:Key="ListViewItemStyle" TargetType="ListViewItem">
        <Setter Property="Margin" Value="3"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListViewItem">
                    <Border
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Padding="{TemplateBinding Padding}">
                        <ContentPresenter/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="BorderBrush" Value="Red"/>
            </Trigger>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="LightBlue"/>
            </Trigger>
        </Style.Triggers>
    </Style>
    

    The item will be displayed differently when the mouse is over (background color) and when the item is selected (red border). The foreground should stay untouched with this.

    Usage:

    <ListView ItemContainerStyle="{StaticResource ListViewItemStyle}">
        <!-- your other code -->
    </ListView>