WPF DataGridTemplateColumn cell style not displaying value

走远了吗. 提交于 2021-01-28 11:45:25

问题


I am trying to create a style for datagridcell. But the text value is not displayed.Data grid was populated by the Observable collection of type Student. Please find my xaml code below.

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>

I am getting blank cell instead of value. Kindly let me know where am I doing it wrong.


回答1:


Your template column should be

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

For resuing:

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



回答2:


There are two ways to fix this.

(First one has already been taken care of by Raviraj I see)

Second one would be to actually separate the Cell Style and Content. Your current code doesn't work because the TextBlock is missing a DataContext (not available in the CellStyle itself):

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

You could of course simply use a 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>


来源:https://stackoverflow.com/questions/49189588/wpf-datagridtemplatecolumn-cell-style-not-displaying-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!