我正在尝试为datagridcell创建一个样式。但是不显示文本值。数据网格由Student类型的Observable集合填充。请在下面找到我的xaml代码。
StdCollection = new ObservableCollection<Student>()
{
new Student(){Name="Ram",StudID=01,Total=88},
new Student(){Name="John",StudID=02,Total=55},
new Student(){Name="Amar",StudID=03,Total=77},
new Student(){Name="Azar",StudID=04,Total=98},
};
<DataGrid Name="dgTest" CanUserAddRows="False" CanUserResizeColumns="True" CanUserSortColumns="True" IsReadOnly="False"
HorizontalAlignment="Left" Height="194" Margin="37,36,0,0" VerticalAlignment="Top" Width="236"
ItemsSource="{Binding StdCollection,UpdateSourceTrigger=PropertyChanged}"
AutoGenerateColumns="False"
>
<DataGrid.Columns>
<DataGridTemplateColumn Header="StudID" >
<DataGridTemplateColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border x:Name="border"
Background="WhiteSmoke"
BorderBrush="Transparent"
BorderThickness="2"
SnapsToDevicePixels="True">
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
<ContentPresenter.ContentTemplate>
<DataTemplate>
<Border BorderBrush="Red" BorderThickness="1">
<TextBlock Foreground="Gray" FontSize="18" Text="{Binding StudID}"/>
</Border>
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGridTemplateColumn.CellStyle>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Name" Binding="{Binding Name,UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn Header="Total" Binding="{Binding Total,UpdateSourceTrigger=PropertyChanged}"/>
</DataGrid.Columns>
我得到空白单元而不是值。 请告诉我在哪里做错了。
答案 0 :(得分:1)
您的模板列应为
<DataGridTemplateColumn Header="Id" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Border x:Name="border"
Background="WhiteSmoke"
BorderBrush="Transparent"
BorderThickness="2"
SnapsToDevicePixels="True">
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
<ContentPresenter.ContentTemplate>
<DataTemplate>
<Border BorderBrush="Red" BorderThickness="1">
<TextBlock Foreground="Gray" FontSize="18" Text="{Binding StudID}"/>
</Border>
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
</Border>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
重新开始:
<Window.Resources>
<DataTemplate x:Key="columnTemplate">
<Border BorderBrush="Red" BorderThickness="1">
<TextBlock Foreground="Gray" FontSize="18" Text="{Binding Id}"/>
</Border>
</DataTemplate>
</Window.Resources>
<Grid x:Name="MainGrid">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<DataGrid Name="dgMainGrid" CanUserAddRows="False" CanUserResizeColumns="True" CanUserSortColumns="True" IsReadOnly="False"
HorizontalAlignment="Left" Height="194" Margin="37,36,0,0" VerticalAlignment="Top" Width="236"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Id" CellTemplate="{StaticResource columnTemplate}"/>
<DataGridTextColumn Header="Name" Binding="{Binding Description, UpdateSourceTrigger=PropertyChanged}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
答案 1 :(得分:0)
有两种方法可以解决这个问题。
(第一个已经由Raviraj照顾过我看到了)
第二个实际上是将细胞样式和内容分开。您当前的代码无法正常工作,因为TextBlock缺少DataContext(在CellStyle本身中不可用):
<DataGridTemplateColumn Header="StudID">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Foreground="Gray" FontSize="18" Text="{Binding StudID}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border x:Name="border"
Background="WhiteSmoke"
BorderBrush="Transparent"
BorderThickness="2"
SnapsToDevicePixels="True">
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
<ContentPresenter.ContentTemplate>
<DataTemplate>
<Border BorderBrush="Red" BorderThickness="1">
<ContentPresenter Content="{TemplateBinding Content}"/>
</Border>
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGridTemplateColumn.CellStyle>
</DataGridTemplateColumn>
您当然可以简单地使用TextColumn:
<DataGridTextColumn Binding="{Binding StudID}" Header="StudID" FontSize="18" Foreground="Gray">
<DataGridTextColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border x:Name="border"
Background="WhiteSmoke"
BorderBrush="Transparent"
BorderThickness="2"
SnapsToDevicePixels="True">
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
<ContentPresenter.ContentTemplate>
<DataTemplate>
<Border BorderBrush="Red" BorderThickness="1">
<ContentPresenter Content="{TemplateBinding Content}"/>
</Border>
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>