Results 1 to 5 of 5

Thread: ListView Background Color

  1. #1

    Thread Starter
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    ListView Background Color

    I have a ListView where one element of the Collection is a color and the element is called "ItemColor" (of type Color). I want to set a given ListViewItem to be that color.

    No matter what I try, I can't get this to work. In the example below, the Background is being set to a static color, but I want this to be something like Value= Binding(ItemColor) and the background of that item to show as being that color.

    Code:
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=RowSelected}" Value="True">
                    <Setter Property="Background" Value="Gainsboro"  />
                    <Setter Property="FontWeight" Value="Bold" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
    Any help appreciated.


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  2. #2

    Thread Starter
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: ListView Background Color

    Code:
    <TextBlock Text="{Binding MyColor}" Width="80" >
        <TextBlock.Background>
            <SolidColorBrush Color="{Binding MyColor}" />
        </TextBlock.Background>
    </TextBlock>
    This also doesn't work. the Background remains the default color.


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  3. #3

    Thread Starter
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: ListView Background Color

    I got to the bottom of this and for posterity the solution is;

    Assuming MyCollection has a member "Color", create an additional string member such as MyCollection.KnownColor and set it to MyCollection.Color.Name.ToString(). Then use the XML below.

    Code:
    <ListView VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Name ="ListBoxResults"  
    ItemsSource="{Binding Path=MyCollection}" SelectionMode="Single">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
    	    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    	</Style>
        </ListView.ItemContainerStyle>
        <ListView.View>
    	<GridView>
    	  <GridViewColumn Header =" Colour "  Width="60" >
    	    <GridViewColumn.CellTemplate>
    	       <DataTemplate>
    		 <TextBlock Text=Color>
    			<TextBlock.Background>
    		 	<SolidColorBrush Color="{Binding KnownColor}" />
    			</TextBlock.Background>
    		</TextBlock>
    		</DataTemplate>
    		</GridViewColumn.CellTemplate>
    		 </GridViewColumn>
    		<GridViewColumn Header=" Size "  Width="60">
    		<GridViewColumn.CellTemplate>
    		<DataTemplate>
    		<TextBlock  Text="{Binding AnOther}" HorizontalAlignment="Center" />
    		</DataTemplate>
    		</GridViewColumn.CellTemplate>
    		</GridViewColumn>
    		</GridView> 
    </ListView.View> 
    </ListView>


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  4. #4
    New Member
    Join Date
    Nov 2015
    Location
    Russia
    Posts
    1

    ListView Background

    If you want a transparent box for the game cards, leave the background field empty, rather than specifying a color. That should make it transparent for you.

    Оффтопик:
    Как правильно variant4
    Milliard Dollars from youtube

  5. #5
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    Re: ListView Background Color

    You should not bind to a color but to a brush, in your case a SolidColorBrush
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width