我的 xaml 中有以下 DataTemplate:
<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>
我遇到的问题是,Pruefwert 的值在文本框中不可见。对于数据模板文本框模板
我在末尾添加了一个额外的 DataGridTextColumn,具有相同的 DataBinding,这里的值是可见的,没有任何问题。所以数据模板肯定存在一些绑定问题。而且我没有收到任何绑定错误。
为了应用 DataTemplate,首先必须有某个要应用它的对象。对于 ContentTemplate,此对象将是 Content 属性的值。但你这个属性里什么都没有,它被设置为null。
参见示例:
<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>