我正在开发一个数据虚拟化解决方案,该解决方案由Bea 在她的博客上提供。此解决方案用于与ListView一起工作。我只是想让它与WPF DataGrid一起工作,但我的尝试没有成功。
这里是她提供的完整工作示例的代码。
提供的代码中的ListView绑定到自定义AsyncVirtualizingCollection集合class。但是,当我试图将这个集合绑定到DataGrid时,它确实会显示空行。数据不会出现在DataGrid中。
现在,魔术发生在为ControlTemplate编写ListViewItem的方式上。我已经把它归零到了ContentPresenter中的ControlTemplate。ContentPresenter已被作为ContentPresenter的Border所取代。下面是相同的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时,您可能会注意到: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。
发布于 2013-09-20 10:32:53
由于DataWrapper是您的DataGrid的项,它正在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}" />https://stackoverflow.com/questions/18911965
复制相似问题