4

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 ....

WiiMaxx
  • 5,322
  • 8
  • 51
  • 89
  • 1
    I don't understand your question... why would you need to set the `DataContext` of the `DataGridTemplateColumn`? That is already implicitly done by the `DataGrid` control, isn't it? In the `DataTemplate`, you should already have access to the properties of the object(s) in the collection that is data bound to the `DataGrid.ItemsSource` property. – Sheridan Nov 20 '13 at 11:05
  • my DataContext is a LIST to access a object i need to specific the item in the list like `[0]` _(to get the first one)_ but if i use a DataTemplate as StaticResource i need to specific the DataContext ... – WiiMaxx Nov 20 '13 at 11:13
  • Is this `List` *not* set as the `DataGrid.ItemsSource` property? *if i use a DataTemplate as StaticResource i need to specific the DataContext* - I don't think that is true. The only time (in this situation) that you'd need to specify the `DataContext` is if you want to `Bind` to a different object to the one in the collection that is data bound to the `DataGrid.ItemsSource` property... is *that* what you're trying to do? – Sheridan Nov 20 '13 at 11:21
  • no its little more complicate my `DataGrid.ItemsSource` is a List which contains 2 Propertys the first one is `MyRowheader` and the second one is `MyCellList` which is a `List` – WiiMaxx Nov 20 '13 at 11:26
  • And you just want to show one value from `MyCellList` in each row of your `DataGrid`? If that's right, is it the *same* value, or a different value from `MyCellList` on each row? – Sheridan Nov 20 '13 at 11:33
  • its the same value on each row – WiiMaxx Nov 20 '13 at 12:02

1 Answers1

1

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.

Community
  • 1
  • 1
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • ok now you got what i want to achieve but i want to remove the repetition because basically its always the same code. If you take a close looka the XAML you will see your answer is what i already do (ObColl[1] == MyCellListl[1]). I'm afraid but my english isn't good enough to describe the problem in a better way – WiiMaxx Nov 20 '13 at 13:07
  • I took that code from your example, so yes that bit is the same. I'm still struggling to fully understand what you want. Can you explain what you mean by 'remove the repetition' please? – Sheridan Nov 20 '13 at 13:16
  • ok let me try again the Datatemplate will allway be the same the only thing which will change is the index in my ObColl[??] because the DataContext is the MyRow object so i try to specifie the `DataContext` to get the `MyCell` as `DataContext` for the `Datatemplate` – WiiMaxx Nov 20 '13 at 14:02
  • Oh... when I asked you *is it the same value, or a different value from `MyCellList` on each row*, you said *the same value*... are you now saying that each row should display a different value from the `MyCellList` collection? – Sheridan Nov 20 '13 at 14:40
  • Noo ... each column needs the new Index not the row (i will edit my question) – WiiMaxx Nov 20 '13 at 14:43
  • please take a look at my Edit – WiiMaxx Nov 20 '13 at 14:54