Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

I am having a hard time figuring out how to apply a style to a selected row so that when I click on the row/record I am able to highlight its entire length. More importantly, I need to do this purely in WPF so I can just apply the style once. However, it seems to me that through experimentation, here, C# seems to have an edge on WPF - and this is not what I want. I want to do this purely in WPFY. Any thoughts?
Posted

It is not clear, from your question which control you are talking about.

This[^] article shows how to for a ListView. If you are using a different control then I found this from a search on wpf highlight selected row, just add the type of control to the end and I'm sure that you will find a solution.
 
Share this answer
 
Comments
Isaiah83 13-Nov-10 12:12pm    
It would be for a DataGrid and many of the articles out there are using both C# and XAML. Id like a solution that uses XAML only so I can apply this as a style.
use triggers to do wpf highlight selected row or use MouseEnter and MouseLeave events to change colors
 
Share this answer
 
See here[^].
 
Share this answer
 
I agree with what Class Coder said. well if you want to see the implementation, here it is:

XML
<Style x:Key="ControlListBoxItem" TargetType="ListBoxItem">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <Border Name="ListItemBorder" CornerRadius="3" BorderThickness="1" BorderBrush="Black">
                    <ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="ListItemBorder" Property="BorderBrush" Value="Blue"/>
                        <Setter TargetName="ListItemBorder" Property="TextBlock.FontSize" Value="12"/>
                    </Trigger>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter TargetName="ListItemBorder" Property="TextBlock.Foreground" Value="White"/>
                        <Setter TargetName="ListItemBorder" Property="BorderBrush" Value="Red"/>
                        <Setter TargetName="ListItemBorder" Property="Background" Value="Green"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <!-- The Margin property below decides the margin between each ListBox Items -->
    <Setter Property="Margin" Value="1,1,0,1" />
</Style>


You can ignore the control template part if you don't want to style the listbox items.
Hope it helps! :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900