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. Datagrid Row background binding issue

Datagrid Row background binding issue

Scheduled Pinned Locked Moved WPF
wpfhelpcsharpcsswcf
6 Posts 3 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.
  • E Offline
    E Offline
    eli15021979
    wrote on last edited by
    #1

    Hi, I've stated to learn WPF and facing some binding problems when trying to bind data to data grid. My ViewModel contains an observable collection which bounds to a data grid. Each item in the collection contains a user name and other additional info. One of the requirements is to display each item in the data grid, while each user will have its own color(which is part of the additional info). For some reason I can't find a way to bind the row background color. My XAML looks like this:

    Can anyone help me with this issue? Thanks, Best regards, Eli

    A W 2 Replies Last reply
    0
    • E eli15021979

      Hi, I've stated to learn WPF and facing some binding problems when trying to bind data to data grid. My ViewModel contains an observable collection which bounds to a data grid. Each item in the collection contains a user name and other additional info. One of the requirements is to display each item in the data grid, while each user will have its own color(which is part of the additional info). For some reason I can't find a way to bind the row background color. My XAML looks like this:

      Can anyone help me with this issue? Thanks, Best regards, Eli

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      Use the Background and AlternatingRowBackground properties.

      E 1 Reply Last reply
      0
      • A Abhinav S

        Use the Background and AlternatingRowBackground properties.

        E Offline
        E Offline
        eli15021979
        wrote on last edited by
        #3

        Hi Background and AlternativeRowBackground isn't good enough. As I mentioned before , the color of each row shall be retreived from the model(stored in the database). Also,there can be more than 2 users,and each user have it's own color. Tanks for your help, Eli

        1 Reply Last reply
        0
        • E eli15021979

          Hi, I've stated to learn WPF and facing some binding problems when trying to bind data to data grid. My ViewModel contains an observable collection which bounds to a data grid. Each item in the collection contains a user name and other additional info. One of the requirements is to display each item in the data grid, while each user will have its own color(which is part of the additional info). For some reason I can't find a way to bind the row background color. My XAML looks like this:

          Can anyone help me with this issue? Thanks, Best regards, Eli

          W Offline
          W Offline
          Wayne Gaylard
          wrote on last edited by
          #4

          You will need to create a RowStyle for your DataGrid and bind the Background property to the ColourProperty on your class. Something like this:-

          <DataGrid ItemsSource="{Binding Path=PostCollection}" AutoGenerateColumns="False">
          <DataGrid.RowStyle>
          <Style TargetType="DataGridRow">
          <Setter Property="Background" Value="{Binding Path=RowColour}"/>
          </Style>
          </DataGrid.RowStyle>

                  <DataGrid.Columns>
                      <DataGridTextColumn Header="UserName" Binding="{Binding UserName}"/>
                      <DataGridTextColumn Header="Status" Binding="{Binding Status}"/>
                  </DataGrid.Columns>
          
              </DataGrid>
          

          This obviously assumes the colour property of your user class is called RowColour and that it returns a Brush type. If not you will need to provide a converter if your property returns a Color or string type. Hope this helps

          When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

          E 1 Reply Last reply
          0
          • W Wayne Gaylard

            You will need to create a RowStyle for your DataGrid and bind the Background property to the ColourProperty on your class. Something like this:-

            <DataGrid ItemsSource="{Binding Path=PostCollection}" AutoGenerateColumns="False">
            <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
            <Setter Property="Background" Value="{Binding Path=RowColour}"/>
            </Style>
            </DataGrid.RowStyle>

                    <DataGrid.Columns>
                        <DataGridTextColumn Header="UserName" Binding="{Binding UserName}"/>
                        <DataGridTextColumn Header="Status" Binding="{Binding Status}"/>
                    </DataGrid.Columns>
            
                </DataGrid>
            

            This obviously assumes the colour property of your user class is called RowColour and that it returns a Brush type. If not you will need to provide a converter if your property returns a Color or string type. Hope this helps

            When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

            E Offline
            E Offline
            eli15021979
            wrote on last edited by
            #5

            It sure did...:thumbsup: Wow...I guess WPF is much more difficult than i thought. Thanks a lot.. Best regards, Eli

            W 1 Reply Last reply
            0
            • E eli15021979

              It sure did...:thumbsup: Wow...I guess WPF is much more difficult than i thought. Thanks a lot.. Best regards, Eli

              W Offline
              W Offline
              Wayne Gaylard
              wrote on last edited by
              #6

              Glad to help :)

              eli15021979 wrote:

              Wow...I guess WPF is much more difficult than i thought.

              Once you get the hang of it, it becomes second nature, then you will be amazed at what you can accomplish with it.:thumbsup: Good luck.

              When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

              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