hattenn
hattenn

Reputation: 4399

Advanced templating of DataGrid specific to the data in hand

Let's say I have an object that is like:

Id = 123456789
Date = 01.01.2013
CurrentItems = 20
TotalItems = 200
A = 25

I would like to create a DataGrid that is styled similar to the following image:

Sample image of desired style.

I know how to create a control template for DataGrid to change general cell style, but how can I create a template that is specifically for a given data like this?

Upvotes: 1

Views: 49

Answers (1)

denis morozov
denis morozov

Reputation: 6316

you could use converters, multi bindings, triggers or template selectors... or a mixture of both

<GridViewColumn>
  <GridViewColumn.CellTemplate>
      <DataTemplate>
              <ContentControl ContentTemplateSelector="{StaticResource myCellTemplateSelector}" />
      </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

here's multiple bindings on the item of the collection: (I am free typing here, not VS so sorry if there are missed tags etc..) this one to display 20/200:

<GridViewColumn>
  <GridViewColumn.CellTemplate>
      <DataTemplate>
         <Border Background="{Binding MyItems.A}, Converter={StaticResource BlueToRed}">
           <StackPanel Orientation="Horizontal">
             <TextBlock Text="{Binding MyItems.CurrentItems}"/>
             <TextBlock Text="/"/>
             <TextBlock Text="{Binding MyItems.TotalItems}"/>
           </StackPanel>
         </Border>
      </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

Upvotes: 1

Related Questions