首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ControlTemplate for DataGridCell

ControlTemplate for DataGridCell
EN

Stack Overflow用户
提问于 2013-09-20 08:19:19
回答 1查看 5.2K关注 0票数 1

我正在开发一个数据虚拟化解决方案,该解决方案由Bea 在她的博客上提供。此解决方案用于与ListView一起工作。我只是想让它与WPF DataGrid一起工作,但我的尝试没有成功。

这里是她提供的完整工作示例的代码。

提供的代码中的ListView绑定到自定义AsyncVirtualizingCollection集合class。但是,当我试图将这个集合绑定到DataGrid时,它确实会显示空行。数据不会出现在DataGrid中。

现在,魔术发生在为ControlTemplate编写ListViewItem的方式上。我已经把它归零到了ContentPresenter中的ControlTemplateContentPresenter已被作为ContentPresenterBorder所取代。下面是相同的XAML

代码语言:javascript
复制
<Style TargetType="{x:Type ListViewItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <Border BorderThickness="{TemplateBinding BorderThickness}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        Background="{TemplateBinding Background}"
                        CornerRadius="2"
                        SnapsToDevicePixels="true">
                    <Border Name="InnerBorder" CornerRadius="1" BorderThickness="1">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition MaxHeight="11"/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <Rectangle Name="UpperHighlight" Visibility="Collapsed" Fill="#75FFFFFF"/>
                            <GridViewRowPresenter Grid.RowSpan="2"
                                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                                    Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Data}"/>
                            <StackPanel Name="Loading" Orientation="Horizontal" Grid.RowSpan="2" Visibility="Collapsed">
                                <TextBlock Text="Loading item " />
                                <TextBlock Text="{Binding ItemNumber}" />
                                <TextBlock Text="..." />
                            </StackPanel>
                        </Grid>
                    </Border>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

现在,仔细查看第二个边框内的GridViewRowPresenter时,您可能会注意到:Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Data}"

我认为这是帮助显示数据的一行。

我尝试为ControlTemplate创建一个DataGridCell,如下所示:

代码语言:javascript
复制
<Style TargetType="{x:Type DataGridCell}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="DataGridCell" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
                <Border BorderThickness="{TemplateBinding Border.BorderThickness}" 
                    BorderBrush="{TemplateBinding Border.BorderBrush}" 
                    Background="{TemplateBinding Panel.Background}" 
                    SnapsToDevicePixels="True">
                    <ContentPresenter Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Data}" 
                        ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
                        ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" 
                        SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

但是我仍然没有在DataGrid中得到任何数据。

我不知道我在这里错过了什么,因为我不擅长模板和样式。

为了避免太无聊,我尽量保持这一点的精确性。我想我已经错过了任何信息请一定要问,我会非常乐意提供的。

任何帮助都将不胜感激。

注意:链接的应用程序以.Net 3.5为目标,需要转换为.Net 4.0才能使用DataGrid。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-20 10:32:53

由于DataWrapper是您的DataGrid的项,它正在Data属性中包装实际的项,因此您应该更新列绑定如下:

代码语言:javascript
复制
<DataGrid Grid.Row="2" ItemsSource="{Binding}" AutoGenerateColumns="False" 
                       IsSynchronizedWithCurrentItem="True" Name="dg"  
                       HorizontalAlignment="Center" Margin="5" Height="300" Width="400"> 
     <DataGrid.Columns> 
           <DataGridTextColumn Header="Id" Binding="{Binding Data.Id}" Width="100"/>  
           <DataGridTextColumn Header="Name" Binding="{Binding Data.Name}" Width="100"/>  
     </DataGrid.Columns> 
</DataGrid>

类似地,您必须在ControlTemplate中设置绑定,如:

代码语言:javascript
复制
<ContentPresenter Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" />
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18911965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档