Question

Currently i bind to a List<T> so i have to do specific set foreach Column a separate DataTemplate

like this:

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
       <TextBlock TextAlignment="Center" 
                  Text="{Binding ObColl[1].Std, UpdateSourceTrigger=PropertyChanged}"
                  Background="{Binding ObColl[1].DienstColor, TargetNullValue=Transparent,FallbackValue=Transparent}" />
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

but i want is to create the DataTemplate one time as Resources

<DataGrid.Resources>
        <DataTemplate x:Name="MyCellTemplate">
           <TextBlock TextAlignment="Center" 
                      Text="{Binding Std, UpdateSourceTrigger=PropertyChanged}"
                      Background="{Binding DienstColor, TargetNullValue=Transparent,FallbackValue=Transparent}" />
        </DataTemplate>
</DataGrid.Resources>

and use it like

<DataGridTemplateColumn CellTemplate="{StaticResource MyCellTemplate} ??{Binding ObColl[1]}??"/>

But to do so i need to specific the DataContext (ObColl[Idx]) in my DataGridTemplateColumn but how do i do this?


EDIT

the xaml should look like :

    <DataGrid Name="dataGrid1"
              ItemsSource="{Binding Itemlist, UpdateSourceTrigger=PropertyChanged}">
        <DataGrid.Resources>
            <DataTemplate x:Key="MyCellTemplate">
                <TextBlock TextAlignment="Center" 
                           Text="{Binding Std, UpdateSourceTrigger=PropertyChanged}" 
                            Background="{Binding DienstColor, TargetNullValue=Transparent, FallbackValue=Transparent}" />
            </DataTemplate>

        </DataGrid.Resources>

        <DataGrid.Columns> 
            <!-- Column 1 -->
            <DataGridTemplateColumn CellTemplate="{StaticResource MyCellTemplate}" 
                                    DataContext={Binding ObColl[0]}/> 
            <!-- Column Header 2 -->
            <DataGridTemplateColumn CellTemplate="{StaticResource MyCellTemplate}" 
                                    DataContext={Binding ObColl[1]}/> 
        </DataGrid.Columns>
    </DataGrid>

the DataContext={Binding ObColl[1]} is the problem part because it doesn't exist ....

Was it helpful?

Solution

Ok, here is my understanding of you requirement... you have a MyRow class with two properties; MyRowheader and MyCellList. You want to display the MyRowheader value and one value from the MyCellList collection on each row of your DataGrid. This is how I would do that:

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding YourCollection}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Header" Binding="{Binding MyRowheader, 
UpdateSourceTrigger=PropertyChanged}" />
        <DataGridTemplateColumn Header="Cell list">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding MyCellList[1].Std, UpdateSourceTrigger=
PropertyChanged}" Background="{Binding MyCellListl[1].DienstColor, TargetNullValue=
Transparent, FallbackValue=Transparent}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

Please let me know if I have misunderstood your requirement.


UPDATE >>>

So I did misunderstand your requirement. It seem as though you want one value from your MyCellList collection in each column, not row, of the DataGrid. In that case, my answer would be no, you can't setup your DataGrid.Columns using a DataTemplate or any other XAML saving feature. XAML is a verbose language... there are a few ways of writing it more efficiently, but not many. You will often find repeated code on XAML pages.

The only way that I can think of that you could write less code would be if you dynamically generated the columns from code. You can find a basic example of that in the Dynamically add Columns to DataGrid in wpf post. I don't know how much time that will save you though really.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top