0

I've got a DataTemplate for a DataGridTemplateColum wich looks like this:

                <toolkit:DataGridTemplateColumn  x:Name="DataGridTextColumnIstVorvorjahr"  IsReadOnly="True" SortMemberPath="SummeIstVorvorjahr">
                <toolkit:DataGridTemplateColumn.CellTemplate >
                    <DataTemplate>
                        <Grid HorizontalAlignment="Stretch" Background="Transparent" Margin="0,-5">
                            <DockPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                                <TextBlock Panel.ZIndex="100" Style="{DynamicResource CellText}" Text="{Binding Path=SummeIstVorvorjahrGerundet, Converter={StaticResource numberFormatter}, ConverterParameter='#,0.0 T€'}"  DockPanel.Dock="Right"/>
                                <Image Panel.ZIndex="90" DockPanel.Dock="Left" MouseLeftButtonUp="FilterDataGridAnalyse_MouseDoubleClick" HorizontalAlignment="Left" Margin="5,0,0,0" Width="20" Height="20" Visibility="Hidden" Name="ImageNormal"  Source="pack://application:,,,/Cis.Common.Presentation;component/Resources/Images/Lupe.png" />
                            </DockPanel>
                        </Grid>
                        <DataTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter TargetName="ImageNormal" Property="Visibility" Value="Visible" />
                            </Trigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </toolkit:DataGridTemplateColumn.CellTemplate>
                <toolkit:DataGridTemplateColumn.HeaderTemplate>
                    <DataTemplate >
                        <DockPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch" LastChildFill="False">

                            <TextBlock x:Name="TextBlockHeaderZeile1" Text="Ist" DockPanel.Dock="Top" />
                            <WrapPanel DockPanel.Dock="Top">
                                <TextBlock x:Name="TextBlockHeaderZeile2" Text=""/>
                                <ContentPresenter x:Name="contentPresenter">
                                    <ContentPresenter.Content>
                                        <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Content" />
                                    </ContentPresenter.Content>
                                </ContentPresenter>
                            </WrapPanel>
                            <Border Style="{DynamicResource borderline}">
                                <TextBlock VerticalAlignment="Stretch" x:Name="TextBlockSumme" Text="{Binding Path=KumulierteSummeIstVorvorjahr, Converter={StaticResource numberFormatter}, ConverterParameter='#,0.0 T€',  RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type cis:ChildWindow}}}"
                                       />
                            </Border>
                        </DockPanel>
                    </DataTemplate>
                </toolkit:DataGridTemplateColumn.HeaderTemplate>
            </toolkit:DataGridTemplateColumn>

Now I want to make a StandartTemplate for this Type because I've got many Colums like this, with only differ in the bindings of the texts in the colums as well as in their headers.

As far I've tried to make a Style for this, but this won't work, I tried to create an usercontrol (but I think it's like taking a sledgehammer to crack a nut).

So any help or hint how to solve this problem would be appreciated.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Tokk
  • 4,499
  • 2
  • 31
  • 47

3 Answers3

1

Add the DataTemplate into the Resources and then access it via a StaticResource

<Window>
    <Window.Resources>
        <DataTemplate x:Key="MyColumnTemplate">
            ...
        </DataTemplate>
        <DataTemplate x:Key="MyColumnTemplateHeader">
            ...
        </DataTemplate>
    </Window.Resources>

    ...

    <toolkit:DataGridTemplateColumn x:Name="DataGridTextColumnIstVorvorjahr"  IsReadOnly="True" SortMemberPath="SummeIstVorvorjahr"
        CellTemplate={StaticResource MyColumnTemplate}
        HeaderTemplate={StaticResource MyColumnTemplateHeader}

    ...
</Window>
Rachel
  • 130,264
  • 66
  • 304
  • 490
  • well my Problem is that I have multiple colums with differ in the bindings of the texts. But I cant say in the DataTemplate Text={TemplateBinding TextXX}... – Tokk Nov 04 '10 at 02:14
  • I'm sure there's a way around that... for example, you could use DataGridTextColumns to bind the data, then in the Style for that column overwrite the template to use your own DataTemplate and access the binding via a RelativeSource. Haven't tested the syntax, but something like `{Binding RelativeSource={AncestorType={x:Type DataGridTextColumn}}, Path=Binding}`. You could also use a custom DependencyProperty on the DataGridTemplateColumn to store the binding instead a DataGridTextColumn. – Rachel Nov 04 '10 at 12:15
1

I don't see why you've rejected the UserControl approach. UserControls are pretty lightweight. They add very little overhead at runtime. They are an extra feature in your project of course, but I usually find that to be an improvement - WPF projects with a small number of large Xaml files are typically hard to maintain.

Far from being a 'sledgehammer', they seem like exactly the right solution here to me.

Ian Griffiths
  • 14,302
  • 2
  • 64
  • 88
0

If I understand you, you try to bind the same column template with different data and to have different header's content relative with column data. So, you may use "dynamic XAML" (XAML used in C# code - that is dynamic) which allows you to use one template for different data.

Here a simple example.

In C# code we create DataGridTemplateColumn object:

DataGridTemplateColumn tc = new DataGridTemplateColumn();

Then we set the CellTemplate property with template which is created dynamically in special function:

tc.CellTemplate = (DataTemplate)XamlReader.Parse(GetTextCellDataTemplate(someText));

Here is a special function which creates our template:

public static string GetTextCellDataTemplate(string bindingPath)
        {
            return @"                
                <DataTemplate
                xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
                xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" >
                    <ScrollViewer MaxHeight=""200"" MaxWidth=""250"" VerticalScrollBarVisibility=""Auto"">
                        <TextBlock Text=""{Binding Path=" + bindingPath + @"}""
                                   TextWrapping=""Wrap"" />
                    </ScrollViewer>
                </DataTemplate>";
        }

Now you may send various information in this function as text and get the same template. You may choose the template from the information you want to put in the cell. For this you must write various function which will return various templates.

The same approach can be applied to header template.

user808128
  • 511
  • 1
  • 7
  • 24