Skip to content

Commit 0249b62

Browse files
committed
feat: AutoLoad on DataContext changed to support DataGrid
1 parent 13482fe commit 0249b62

3 files changed

Lines changed: 40 additions & 4 deletions

File tree

DataVirtualization/VirtualListItemBase.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,19 @@ public abstract class VirtualListItemBase
1515

1616
static void OnAutoLoadChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
1717
{
18-
DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(ContentControl.ContentProperty, typeof(DependencyObject));
18+
// DataGridRow is not ContentControl
19+
//DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(ContentControl.ContentProperty), typeof(DependencyObject));
20+
DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(FrameworkElement.DataContextProperty, typeof(DependencyObject));
1921
if (dpd == null)
22+
{
2023
return;
24+
}
2125

2226
bool isEnabled = (bool)e.NewValue;
2327
if (isEnabled)
24-
dpd.AddValueChanged(d, OnContentChanged);
28+
dpd.AddValueChanged(d, OnDataContextChanged);
2529
else
26-
dpd.RemoveValueChanged(d, OnContentChanged);
30+
dpd.RemoveValueChanged(d, OnDataContextChanged);
2731
}
2832

2933
static void OnContentChanged(object sender, EventArgs e)
@@ -33,6 +37,13 @@ static void OnContentChanged(object sender, EventArgs e)
3337
item.LoadAsync();
3438
}
3539

40+
static void OnDataContextChanged(object sender, EventArgs e)
41+
{
42+
VirtualListItemBase item = ((DependencyObject)sender).GetValue(FrameworkElement.DataContextProperty) as VirtualListItemBase;
43+
if (item != null)
44+
item.LoadAsync();
45+
}
46+
3647
public static bool GetAutoLoad(DependencyObject d)
3748
{
3849
return (bool)d.GetValue(AutoLoadProperty);

DataVirtualizationDemo/Window1.xaml

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010
<TextBlock Text="{Binding FirstName}" />
1111
</DataTemplate>
1212

13-
<!-- Setting VirtualListItemBase.AutoLoad to true will load the item when it's set to ListViewItem.Content property -->
13+
<!-- Setting VirtualListItemBase.AutoLoad to true will load the item when it's set to ListViewItem.DataContext property -->
1414
<Style TargetType="ListViewItem">
1515
<Setter Property="dz:VirtualListItemBase.AutoLoad" Value="true" />
1616
</Style>
17+
<!-- Setting VirtualListItemBase.AutoLoad to true will load the item when it's set to DataGridRow.DataContext property -->
18+
<Style TargetType="DataGridRow">
19+
<Setter Property="dz:VirtualListItemBase.AutoLoad" Value="true" />
20+
</Style>
1721

1822
<!-- ColumnHeader template to show up and down arrow when sorted -->
1923
<DataTemplate x:Key="ListViewColumnHeaderTemplate">
@@ -80,6 +84,26 @@
8084
</GridView>
8185
</ListView.View>
8286
</ListView>
87+
88+
<DataGrid
89+
x:Name="dataGrid"
90+
EnableRowVirtualization="True"
91+
ScrollViewer.IsDeferredScrollingEnabled="True"
92+
dz:VirtualListLoadingIndicator.IsAttached="True"
93+
Height="200">
94+
<DataGrid.Columns>
95+
<DataGridTextColumn Width="60" Binding="{Binding Data.Id}" Header="Id" SortMemberPath="Id" />
96+
<DataGridTextColumn Width="120" Binding="{Binding Data.FirstName}" Header="First Name" SortMemberPath="FirstName" />
97+
<DataGridTextColumn Width="120" Binding="{Binding Data.LastName}" Header="Last Name" SortMemberPath="LastName" />
98+
<DataGridTemplateColumn Width="100" Header="DataTemplate test">
99+
<DataGridTemplateColumn.CellTemplate>
100+
<DataTemplate>
101+
<ContentControl Content="{Binding Data}" />
102+
</DataTemplate>
103+
</DataGridTemplateColumn.CellTemplate>
104+
</DataGridTemplateColumn>
105+
</DataGrid.Columns>
106+
</DataGrid>
83107

84108
<TextBlock>
85109
<Hyperlink NavigateUri="http://www.devzest.com/blog/post/WPF-Data-Virtualization.aspx" Click="Hyperlink_Click">http://www.devzest.com/blog/post/WPF-Data-Virtualization.aspx</Hyperlink>

DataVirtualizationDemo/Window1.xaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ private void Refresh_Click(object sender, RoutedEventArgs e)
7575
{
7676
VirtualList<Person> data = new VirtualList<Person>(this);
7777
listView.ItemsSource = data;
78+
dataGrid.ItemsSource = data;
7879
}
7980

8081
// This method helps to get dependency property value in another thread.

0 commit comments

Comments
 (0)