Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Custom SelectedItem in ListView

Custom SelectedItem in ListView

Scheduled Pinned Locked Moved WPF
csharpwpfwinformsquestion
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Saksida Bojan
    wrote on last edited by
    #1

    Hello, i am very new to WPF and so far i like it more than WinForms. Currently i have a template for ListViewItems. And i want to change the selected style. When i select an item, i see blue background. Can anyone tell me where shoud i look into?

    P 1 Reply Last reply
    0
    • S Saksida Bojan

      Hello, i am very new to WPF and so far i like it more than WinForms. Currently i have a template for ListViewItems. And i want to change the selected style. When i select an item, i see blue background. Can anyone tell me where shoud i look into?

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      In WPF, the selected item background colour is determined by System.HighlightBrush. This means that you can define a brush for the style and use SystemColors.HighlightBrushKey to set this. Here's a sample:

      <Style TargetType='ListViewItem'>
      <Style.Resources>
      <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />
      </Style.Resources>
      </Style>

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

      My blog | My articles | MoXAML PowerToys | Onyx

      S 1 Reply Last reply
      0
      • P Pete OHanlon

        In WPF, the selected item background colour is determined by System.HighlightBrush. This means that you can define a brush for the style and use SystemColors.HighlightBrushKey to set this. Here's a sample:

        <Style TargetType='ListViewItem'>
        <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />
        </Style.Resources>
        </Style>

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

        My blog | My articles | MoXAML PowerToys | Onyx

        S Offline
        S Offline
        Saksida Bojan
        wrote on last edited by
        #3

        Wow, thank you. I didn't ever knew it was possible to override system colors

        P 1 Reply Last reply
        0
        • S Saksida Bojan

          Wow, thank you. I didn't ever knew it was possible to override system colors

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          You are most welcome. Ain't WPF cool?

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

          My blog | My articles | MoXAML PowerToys | Onyx

          S 1 Reply Last reply
          0
          • P Pete OHanlon

            You are most welcome. Ain't WPF cool?

            "WPF has many lovers. It's a veritable porn star!" - Josh Smith

            As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

            My blog | My articles | MoXAML PowerToys | Onyx

            S Offline
            S Offline
            Saksida Bojan
            wrote on last edited by
            #5

            Pete O'Hanlon wrote:

            Ain't WPF cool?

            I am asking mysely: Why didn't i tried this before Now i have a diffrent problem. I do not think it is alrigt to contiune new post, but here it is: This is a stripped down event Template for a ListViewItem template

            <DataTemplate x:Key="ListBoxItemPodjetjeTemplate">
                <Grid Margin="2">
                    <Border Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" BorderThickness="0" Background="#8098D8D8" CornerRadius="4,4,4,4"/>
                </Grid>
                <DataTemplate.Triggers>
                    <Trigger Property="Control.IsMouseOver">
                        <Setter Property="Control.Background" Value="LightGreen"/>
                    </Trigger>
                </DataTemplate.Triggers>
            </DataTemplate>
            

            I put Highlight color to transparent, now i want to change background of an item based on mouse over. Now the trigger is throwing me an exception with an inner text: {"'{DependencyProperty.UnsetValue}' is not a valid value for property 'IsMouseOver'."}. Thank you in advance

            P 1 Reply Last reply
            0
            • S Saksida Bojan

              Pete O'Hanlon wrote:

              Ain't WPF cool?

              I am asking mysely: Why didn't i tried this before Now i have a diffrent problem. I do not think it is alrigt to contiune new post, but here it is: This is a stripped down event Template for a ListViewItem template

              <DataTemplate x:Key="ListBoxItemPodjetjeTemplate">
                  <Grid Margin="2">
                      <Border Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" BorderThickness="0" Background="#8098D8D8" CornerRadius="4,4,4,4"/>
                  </Grid>
                  <DataTemplate.Triggers>
                      <Trigger Property="Control.IsMouseOver">
                          <Setter Property="Control.Background" Value="LightGreen"/>
                      </Trigger>
                  </DataTemplate.Triggers>
              </DataTemplate>
              

              I put Highlight color to transparent, now i want to change background of an item based on mouse over. Now the trigger is throwing me an exception with an inner text: {"'{DependencyProperty.UnsetValue}' is not a valid value for property 'IsMouseOver'."}. Thank you in advance

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              What value are you applying? You haven't actually set one in the Trigger. Try changing it to

              <Trigger Property="Control.IsMouseOver" Value="True" />

              "WPF has many lovers. It's a veritable porn star!" - Josh Smith

              As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

              My blog | My articles | MoXAML PowerToys | Onyx

              S 1 Reply Last reply
              0
              • P Pete OHanlon

                What value are you applying? You haven't actually set one in the Trigger. Try changing it to

                <Trigger Property="Control.IsMouseOver" Value="True" />

                "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                My blog | My articles | MoXAML PowerToys | Onyx

                S Offline
                S Offline
                Saksida Bojan
                wrote on last edited by
                #7

                Aw stupid me. Thank you for your reply

                P 1 Reply Last reply
                0
                • S Saksida Bojan

                  Aw stupid me. Thank you for your reply

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #8

                  No problems - it's easy to miss something like this. As a hint, if you ever see a message about DependencyProperty.UnsetValue then it means the value the DP expects hasn't been set. This isn't the same as a null value because null might be a legitimate value for the DP.

                  "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                  As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                  My blog | My articles | MoXAML PowerToys | Onyx

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups