WPF Listview Gridview Selected Item Back color

Hi to all,

I have a WPF Listview Gridview where I am trying to set the selected Item to Transparent. I am having a lot of trouble doing that and can't seem to find the right answer online. Can someone please help me with this issue. I am also trying to do the same for the "mouse over" item.
Below is the XAML for my listview:

I am using Visual Studio 2008 and .Net 3.5. I am also using Microsoft Blend 3.

Thanks
<filter:FilterSortListView x:Name="SearchListView" d:LayoutOverrides="Height" Grid.Row="1" 
                                   IsSynchronizedWithCurrentItem="True" Background="{x:Null}" 
                                   BorderBrush="{x:Null}"
                                   VirtualizingStackPanel.VirtualizationMode="Recycling" 
                                   RenderTransformOrigin="0.5,0.5">
            <filter:FilterSortListView.Resources>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Foreground" Value="Red"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </filter:FilterSortListView.Resources>
			<filter:FilterSortListView.Style>
				<StaticResource ResourceKey="ListBoxBackGround"/>
			</filter:FilterSortListView.Style>
            <filter:FilterSortListView.RenderTransform>
                <TransformGroup>
                    <ScaleTransform ScaleX="1" ScaleY="1"/>
                    <SkewTransform AngleX="0" AngleY="0"/>
                    <RotateTransform Angle="0"/>
                    <TranslateTransform X="0" Y="0"/>
                </TransformGroup>
            </filter:FilterSortListView.RenderTransform>
            <filter:FilterSortListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}"  >
                    <Setter Property="ToolTip" Value="{Binding Source={StaticResource memTooltip}}" />                    
                </Style>
            </filter:FilterSortListView.ItemContainerStyle>
            <filter:FilterSortListView.View>
				<GridView x:Name="InfoGrid" AllowsColumnReorder="False">
					<filter:SortListViewColumn Width="175"
                                               Header=" Ordered"
                                               HeaderContainerStyle="{DynamicResource FirstColumnGridViewColumnHeader}" 
                                               CellTemplate="{StaticResource ItemCodeImage}"/>
                    <filter:SortListViewColumn Width="75"
                                               Header=" Price"
                                               HeaderContainerStyle="{DynamicResource FamilyDataGridViewColumnHeader}" 
                                               CellTemplate="{StaticResource ItemPrice}"/>
                    <filter:SortListViewColumn Width="150"
                                               Header="Constituent Name"
                                               HeaderContainerStyle="{DynamicResource FamilyDataGridViewColumnHeader}" 
                                               CellTemplate="{StaticResource constituentName}"/>
				</GridView>
			</filter:FilterSortListView.View>
		</filter:FilterSortListView>

Open in new window

ALawrence007Asked:
Who is Participating?
 
CodeCruiserConnect With a Mentor Commented:
Try this

http://www.wpftutorial.net/ListBoxSelectionBackground.html

the trick is to override the SystemColors.HighlightBrushKey to either Transparent (no selected background) or the color you like.
0
 
CloudedTurtleCommented:
Can you link your functional code as well? What is the purpose of setting the transparency on the selected (or hovered) item? If it is to show the active/potential selection, could use just use a different background color instead of trying to set the transparency?
 
0
 
ALawrence007Author Commented:
CloudedTurtle,
That was what my intent in the end was. I wanted to start with transparency and then take it from there. If you could help me with that I would be very grateful!
Code:

Imports System.Globalization
Imports System.Windows.Threading
Imports System.ComponentModel
Imports System.Windows.Data
Imports System
Imports Lawrence

Public Class FilterSortListView
    Inherits SortListView
    Private Delegate Sub FilterDelegate()
    Private _filter As New Filter

    ''' <summary>
    ''' Get the filter for this control.
    ''' </summary>
    Protected ReadOnly Property Filter() As Filter
        Get
            Return Me._filter
        End Get
    End Property

    ''' <summary>
    ''' Filter the data using the specified filter text.
    ''' </summary>
    Public Sub FilterList(ByVal text As String)
        ' Setup the filter object.
        _filter.Parse(text)

        ' Start an async operation that filters the list.
        Me.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, New FilterDelegate(AddressOf FilterWorker))
    End Sub

    ''' <summary>
    ''' Worker method that filters the list.
    ''' </summary>
    Private Sub FilterWorker()
        ' Get the data the ListView is bound to.
        Dim view As ICollectionView = CollectionViewSource.GetDefaultView(Me.ItemsSource)

        ' Clear the list if the filter is empty, otherwise filter the list.
        view.Filter = If(_filter.IsEmpty, Nothing, New Predicate(Of Object)(AddressOf FilterCallback))
    End Sub

    ''' <summary>
    ''' This is called for each item in the list. The derived classes 
    ''' override this method.
    ''' </summary>
    Protected Overridable Function FilterCallback(ByVal item As Object) As Boolean
        Dim flagMem As flagWithMemImage = TryCast(item, flagWithMemImage)
        If flagMem Is Nothing Then
            Return False
        End If
        If Me.Filter.Matches(flagMem.FlagPsdFlag.CheckNumber) Then
            Return True
        End If
        Return False
    End Function

End Class

''' <summary>
''' Class that parses the filter text.
''' </summary>
Public Class Filter
    ' Parsed data from the filter string.
    Private _filterText As String


    ''' <summary>
    ''' Indicates if the filter is empty.
    ''' </summary>
    Public ReadOnly Property IsEmpty() As Boolean
        Get
            Return String.IsNullOrEmpty(Me._filterText)
        End Get
    End Property

    ''' <summary>
    ''' Return true if the filter contains the specified text.
    ''' </summary>
    Public Function Matches(ByVal text As String) As Boolean
        Return (Me._filterText IsNot Nothing AndAlso text IsNot Nothing AndAlso text.ToLower(CultureInfo.CurrentCulture).Contains(Me._filterText))
    End Function

    ''' <summary>
    ''' Parse the specified filter text.
    ''' </summary>
    Public Sub Parse(ByVal text As String)
        ' Initialize fields.
        Me._filterText = ""

        ' Store the filter text.
        Me._filterText = If(String.IsNullOrEmpty(text), "", text.ToLower(CultureInfo.CurrentCulture).Trim())

    End Sub


End Class


Imports System.Windows.Controls
Imports System.ComponentModel
Imports System.Windows.Data
Imports System.Windows
Imports System


''' <summary>
''' A column in the SortListView object that allows the SortProperty and 
''' SortStyle to be specified in XAML. The SortProperty specifies the 
''' underlying bound property that is used when sorting and SortStyle
''' specifies the resource that is used for the column header.
''' </summary>
Public Class SortListViewColumn
    Inherits GridViewColumn
    Public Property SortProperty() As String
        Get
            Return CStr(GetValue(SortPropertyProperty))
        End Get
        Set(ByVal value As String)
            SetValue(SortPropertyProperty, value)
        End Set
    End Property

    Public Shared ReadOnly SortPropertyProperty As DependencyProperty = _
                            DependencyProperty.Register("SortProperty", GetType(String), GetType(SortListViewColumn))

    Public Property SortStyle() As String
        Get
            Return CStr(GetValue(SortStyleProperty))
        End Get
        Set(ByVal value As String)
            SetValue(SortStyleProperty, value)
        End Set
    End Property

    Public Shared ReadOnly SortStyleProperty As DependencyProperty = _
                            DependencyProperty.Register("SortStyle", GetType(String), GetType(SortListViewColumn))
End Class

Public Class SortListView
    Inherits ListView

    ' The current direction the header is sorted;
    Private _sortDirection As ListSortDirection

    ' The previous column that was sorted.
    Private _previousSortColumn As SortListViewColumn

    ' The current column that is sorted.
    Private _sortColumn As SortListViewColumn

    Protected Overrides Sub OnInitialized(ByVal e As EventArgs)
        ' Handle the event when a header is clicked.
        Me.AddHandler(GridViewColumnHeader.ClickEvent, New RoutedEventHandler(AddressOf OnHeaderClicked))
        MyBase.OnInitialized(e)
    End Sub

    ''' <summary>
    ''' A header was clicked. Sort the associated column.
    ''' </summary>
    Private Sub OnHeaderClicked(ByVal sender As Object, ByVal e As RoutedEventArgs)
        ' Make sure the column is really being sorted.
        Dim header As GridViewColumnHeader = TryCast(e.OriginalSource, GridViewColumnHeader)
        If header Is Nothing OrElse header.Role = GridViewColumnHeaderRole.Padding Then
            Return
        End If

        Dim column As SortListViewColumn = TryCast(header.Column, SortListViewColumn)
        If column Is Nothing Then
            Return
        End If

        ' See if a new column was clicked, or the same column was clicked.
        If _sortColumn IsNot column Then
            ' A new column was clicked.
            _previousSortColumn = _sortColumn
            _sortColumn = column
            _sortDirection = ListSortDirection.Ascending
        Else
            ' The same column was clicked, change the sort order.
            _previousSortColumn = Nothing
            _sortDirection = If((_sortDirection = ListSortDirection.Ascending), ListSortDirection.Descending, ListSortDirection.Ascending)
        End If

        ' Sort the data.
        SortList(column.SortProperty)

        ' Update the column header based on the sort column and order.
        UpdateHeaderTemplate()
    End Sub

    ''' <summary>
    ''' Sort the data.
    ''' </summary>
    Private Sub SortList(ByVal propertyName As String)
        Try
            ' Get the data to sort.
            Dim dataView As ICollectionView = CollectionViewSource.GetDefaultView(Me.ItemsSource)

            ' Specify the new sorting information.
            dataView.SortDescriptions.Clear()
            Dim description As New SortDescription(propertyName, _sortDirection)
            dataView.SortDescriptions.Add(description)

            dataView.Refresh()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error Sorting this field" _
                            , MessageBoxButton.OK _
                            , MessageBoxImage.Error)
        End Try

    End Sub

    ''' <summary>
    ''' Update the column header based on the sort column and order.
    ''' </summary>
    Private Sub UpdateHeaderTemplate()
        Dim headerStyle As Style

        ' Restore the previous header.
        If _previousSortColumn IsNot Nothing AndAlso _previousSortColumn.SortStyle IsNot Nothing Then
            headerStyle = TryCast(Me.TryFindResource(_previousSortColumn.SortStyle), Style)
            If headerStyle IsNot Nothing Then
                _previousSortColumn.HeaderContainerStyle = headerStyle
            End If
        End If

        ' Update the current header.
        If _sortColumn.SortStyle IsNot Nothing Then
            ' The name of the resource to use for the header.
            Dim resourceName As String = _sortColumn.SortStyle + _sortDirection.ToString()

            headerStyle = TryCast(Me.TryFindResource(resourceName), Style)
            If headerStyle IsNot Nothing Then
                _sortColumn.HeaderContainerStyle = headerStyle
            End If
        End If
    End Sub

End Class

Open in new window

0
 
ALawrence007Author Commented:
Exactly what I needed!
Thanks
0
All Courses

From novice to tech pro — start learning today.