Reputation: 17
I have following DataTemplate in my xaml:
<DataGrid x:Name="ChecklistDataGrid" AutoGenerateColumns="False" HorizontalAlignment="Stretch" Foreground="White" VerticalAlignment="Stretch" Background="Transparent"
RowBackground="Transparent" AlternatingRowBackground="Transparent" HorizontalGridLinesBrush="White" VerticalGridLinesBrush="White" CanUserAddRows="False" SelectionUnit="Cell">
<DataGrid.Resources>
<!-- Custom Style for DataGridColumnHeader -->
<Style TargetType="DataGridColumnHeader">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Foreground" Value="White"/>
</Style>
<!-- DataTemplate for Buttons -->
<DataTemplate x:Key="ButtonTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Content="IO" Background="Green" Click="IOButton_Click" HorizontalAlignment="Stretch" Grid.Column="0"/>
<Button Content="NIO" Background="Red" Click="NIOButton_Click" HorizontalAlignment="Stretch" Grid.Column="1"/>
<Button Content="NR" Background="DarkGray" Click="NRButton_Click" HorizontalAlignment="Stretch" Grid.Column="2"/>
</Grid>
</DataTemplate>
<!-- DataTemplate for TextBox with NR Button -->
<DataTemplate x:Key="TextBoxTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBox Text="{Binding Pruefwert, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Background="White" HorizontalAlignment="Stretch" Grid.Column="0" Grid.ColumnSpan="2" TextChanged="TextBox_TextChanged"/>
<Button Content="NR" Background="DarkGray" Click="NRButton_Click" HorizontalAlignment="Stretch" Grid.Column="2"/>
</Grid>
</DataTemplate>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Pruefpunkt" Binding="{Binding Pruefpunkt}" Width="1*"/>
<!-- Wrapping Text Column -->
<DataGridTemplateColumn Header="Content" Width="10*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Content}" TextWrapping="Wrap" Background="{Binding ContentBackground}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- Conditional Column for Buttons or TextBox -->
<DataGridTemplateColumn Header="Actions" Width="3*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ContentControl>
<ContentControl.Style>
<Style TargetType="ContentControl">
<Style.Triggers>
<DataTrigger Binding="{Binding Type}" Value="button">
<Setter Property="ContentTemplate" Value="{StaticResource ButtonTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding Type}" Value="text">
<Setter Property="ContentTemplate" Value="{StaticResource TextBoxTemplate}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Pruefpunkt" Binding="{Binding Pruefwert}" Width="1*"/>
</DataGrid.Columns>
</DataGrid>
The problem i encounter, that the value of Pruefwert is not visible in the text box. For the DataTemplate TextBoxTemplate
I've added at the end an extra DataGridTextColumn, with the same DataBinding, here the value is visible without any issues. So there must be some Binding issue withe the dataTemplate. Also i'm not getting any Binding errors.
Upvotes: 0
Views: 31
Reputation: 7978
In order for a DataTemplate to be applied, there must first be some object to which it is applied. For a ContentTemplate, this object will be the value of the Content property. But you have nothing in this property, it is set to null.
See example:
<StackPanel>
<FrameworkElement.Resources>
<DataTemplate x:Key="point.template" DataType="Point">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding X}" Margin="5"/>
<TextBlock Text="{Binding Y}" Margin="5"/>
</StackPanel>
</DataTemplate>
</FrameworkElement.Resources>
<FrameworkElement.DataContext>
<Point X="123" Y="456"/>
</FrameworkElement.DataContext>
<ContentControl ContentTemplate="{DynamicResource point.template}"/>
<ContentControl ContentTemplate="{DynamicResource point.template}"
Content="{Binding}"/>
</StackPanel>
Upvotes: 0