ControlTemplate for DataGridCell

时间:2013-09-20 08:19:20

标签: wpf xaml controltemplate wpf-4.0 contentpresenter

我正在研究 Bea Stollnitz 在她blog上提供的数据虚拟化解决方案。提供此解决方案以使用ListView。我只是想让它与 WPF DataGrid一起使用,但我的尝试并不成功。

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

提供的代码中的ListView绑定到自定义AsyncVirtualizingCollection集合class。但是当我尝试将此集合绑定到 DataGrid 时,它会显示空白行。数据未显示在DataGrid中。

现在神奇的事情发生在为ControlTemplate编写ListViewItem的方式。我已将其归零ContentPresenter中的ControlTemplateContentPresenter已替换为Border,表现为ContentPresenter。以下是 XAML

<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中的Grid,您可能会注意到:Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Data}"

我认为这是有助于显示数据的行。

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

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

1 个答案:

答案 0 :(得分:2)

由于DataWrapperDataGrid的项目,它正在包裹Data属性中的实际项目,因此您应该更新列绑定,如:

<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中设置绑定,如:

<ContentPresenter Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" />