views:

532

answers:

1

I'm learning Silverlight (from scratch, I'm feeling a bit like a fish out of water here!). I'm having a look at the DataGrid class, and am playing around with custom templates for columns and column headers.

I want to display a grid that has a collection of columns that have a small image in the header (different image for each column), and display-only values in the cells, with each column's values being bound to a different property on the bound data.

I've done some reading and got it to work for one column with the xaml below. What I want to do is bundle this column up into some kind of reusable column, and then just add multiple instances of them in my grid, specifying values to define what image to use and what property to bind to.

Can someone help me out with some suggestions? I'm using Silverlight 3.0, btw.

here's the xaml I'm using for one column:

<data:DataGrid x:Name="_bidGrid" IsReadOnly="true" CanUserResizeColumns="False">
    <data:DataGrid.Columns>
        <data:DataGridTemplateColumn>
            <data:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding <bound property name goes here>}"/>
                </DataTemplate>
            </data:DataGridTemplateColumn.CellTemplate>
            <data:DataGridTemplateColumn.HeaderStyle>
                <Style TargetType="dataprimitives:DataGridColumnHeader">
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <Image Source="<image url goes here>"/>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </data:DataGridTemplateColumn.HeaderStyle>
        </data:DataGridTemplateColumn>
    </data:DataGrid.Columns>
</data:DataGrid>
+1  A: 

Hi Niall,

Bundling the actual col into a style would mean you'd have the same image for each col in the grid, you could define a style for each diff col header, then when you define your cols you can reference th style, eg:

<data:DataGridTemplateColumn HeaderStyle="{StaticResource ProductNameColHeadStyle}"

The full XAML below illustrates this better, copy this in to a new SL project MainPage.xaml and run in to see results...

<UserControl x:Class="SilverlightApplication1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 


    xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" 
    xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" 
    xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
    xmlns:System_Windows_Controls_Primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls" 
    xmlns:visualizationToolkit="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    xmlns:System_Windows_Controls_DataVisualization_Charting_Primitives="clr-namespace:System.Windows.Controls.DataVisualization.Charting.Primitives;assembly=System.Windows.Controls.DataVisualization.Toolkit" 
    xmlns:inputToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit" 
    xmlns:dataPrimitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"             
    mc:Ignorable="d" Width="400" Height="300">

    <Grid x:Name="LayoutRoot" Background="White">

        <Grid.Resources> 

            <Style x:Key="ProductNameColHeadStyle"  TargetType="dataPrimitives:DataGridColumnHeader">
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <TextBlock Text="prod name image goes here"/>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <Style x:Key="ProductDescColHeadStyle"  TargetType="dataPrimitives:DataGridColumnHeader">
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <TextBlock Text="prod description image goes here"/>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

        </Grid.Resources>




        <data:DataGrid Name="dta" AutoGenerateColumns="False" >

            <data:DataGrid.Columns>

                <data:DataGridTemplateColumn HeaderStyle="{StaticResource ProductNameColHeadStyle}">                    
                    <data:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="Binding to prod name here"></TextBlock>
                        </DataTemplate>
                    </data:DataGridTemplateColumn.CellTemplate>                    
                </data:DataGridTemplateColumn>

                <data:DataGridTemplateColumn HeaderStyle="{StaticResource ProductDescColHeadStyle}">
                    <data:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="Binding to prod desc here"></TextBlock>
                        </DataTemplate>
                    </data:DataGridTemplateColumn.CellTemplate>
                </data:DataGridTemplateColumn>

            </data:DataGrid.Columns>   

        </data:DataGrid>

    </Grid>


</UserControl>
Jason Roberts
Thanks for the response. What I was hoping for was some way to parameterise the image url and column binding in one XAML package. I have 5 columns of this type that I want to use, so it will lead to 5 styles and 5 template columns that all seem very similar except for the url and the column binding. I guess I could write a class derived from DataGridTemplateColumn and setup the header style and binding in C#, providing properties on the class for the url and binding. But I was hoping to be able to do it with XAML.
Niall Connaughton
No probs Niall, I see where your coming from.. You can end up with a lot of styles\amount of XAML, I found that when I 1st started doing SL\WPF, I think just define the 5 styles in XAML and don't worry too much about the amount of XAML, you could even move the styles into the app.xaml then at least they wont be cluttering up your usercontrol.xaml and they'll be available on other screens. If you were adding dynamic columns or had lots more columns then a derived class would prob be better.
Jason Roberts