Click here to Skip to main content
15,886,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do you style a row in a datagrid bound to an itemssource?
Posted

You can bind the RowStyle to a resource using as
XML
RowStyle="{DynamicResource MyDataGridRow}"
This would be implemented like this
XML
<Style x:Key="ClientSearchResultsViewDataGridRow" TargetType="{x:Type Custom:DataGridRow}">
  <Setter Property="Background" Value="{x:Null}"/>
  <Setter Property="SnapsToDevicePixels" Value="True"/>
  <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
  <Setter Property="ValidationErrorTemplate">
    <Setter.Value>
      <ControlTemplate>
        <TextBlock
          Margin="2,0,0,0"
          VerticalAlignment="Center"
          Foreground="Red"
          Text="!">
          <Run Text="!"/>
        </TextBlock>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type Custom:DataGridRow}">
        <Border
          x:Name="DGR_Border"
          Background="#FF141F29"
          BorderBrush="{TemplateBinding BorderBrush}"
          BorderThickness="{TemplateBinding BorderThickness}"
          SnapsToDevicePixels="True">
          <Custom:SelectiveScrollingGrid>
            <Custom:SelectiveScrollingGrid.ColumnDefinitions>
              <ColumnDefinition Width="0"/>
              <ColumnDefinition Width="*"/>
            </Custom:SelectiveScrollingGrid.ColumnDefinitions>
            <Custom:SelectiveScrollingGrid.RowDefinitions>
              <RowDefinition Height="*"/>
              <RowDefinition Height="Auto"/>
            </Custom:SelectiveScrollingGrid.RowDefinitions>
            <Custom:DataGridCellsPresenter
              Grid.Column="1"
              Background="#FF141F29"
              ItemsPanel="{TemplateBinding ItemsPanel}"
              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
            <Custom:DataGridDetailsPresenter Grid.Column="1" Grid.Row="1" Visibility="{TemplateBinding DetailsVisibility}">
              <Custom:SelectiveScrollingGrid.SelectiveScrollingOrientation>
                <Binding Path="AreRowDetailsFrozen" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type Custom:DataGrid}}">
                  <Binding.ConverterParameter>
                    <Custom:SelectiveScrollingOrientation>Vertical
                    </Custom:SelectiveScrollingOrientation>
                  </Binding.ConverterParameter>
                </Binding>
              </Custom:SelectiveScrollingGrid.SelectiveScrollingOrientation>
            </Custom:DataGridDetailsPresenter>
            <Custom:DataGridRowHeader Grid.RowSpan="2" Custom:SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical">
              <Custom:DataGridRowHeader.Visibility>
                <Binding Path="HeadersVisibility" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type Custom:DataGrid}}">
                  <Binding.ConverterParameter>
                    <Custom:DataGridHeadersVisibility>Row
                    </Custom:DataGridHeadersVisibility>
                  </Binding.ConverterParameter>
                </Binding>
              </Custom:DataGridRowHeader.Visibility>
            </Custom:DataGridRowHeader>
          </Custom:SelectiveScrollingGrid>
        </Border>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>
And that's it - a restyled DataRow.
 
Share this answer
 
v2
Comments
[no name] 13-Jul-11 13:12pm    
Yes. But where? I've tried to put something like this in <datagrid><datagrid.resources>, but it says it'll never be used. Why?
Pete O'Hanlon 13-Jul-11 13:13pm    
What says it won't be used?
[no name] 13-Jul-11 13:19pm    
Visual studio greys it out.
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<DataTrigger+>

</DataTrigger>
</Style.Triggers>
</Style>

</DataGrid.Resources>

Every tag inside datagrid.resources is greyed out.
Pete O'Hanlon 13-Jul-11 13:24pm    
The resolution between the resource and the row isn't perfect in the VS designer - if you had something with a key, it would pick it up no problem (see my answer above). What you could do is move your Style out into the Window.Resources (or UserControl.Resources) and give it a key - then use the {DynamicResource ...}syntax above to reference it (or you could use StaticResource).
shhaubby 4-Oct-11 2:55am    
Can you please explain your code in detail.....mainly highlighting "<selectivescrolinggrid>"
it is really very interesting...
thanks in advance..
Rows? What would it mean? You can customize the styles for columns; this is clear, but rows, especially with data binding to ItemSource… It means that one line will be one style and next line in another, how's that? What should make style of the different rows different, as the content of the row is defined by the data, and the pure data cannot have "style"? Columns are different, because a column in face represents meta-data or the data.

I can only suggest you use grouping. In this way, the rows could be additionally segregated by groups, and you could attribute a different group style to each group.

For a sample, look at this tutorial: http://www.wpftutorial.net/DataGrid.html[^]. Look at the section "Grouping" and its XAML sample, pay attention at DataGrid.GroupStyle.

—SA
 
Share this answer
 
v3

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