/* Based on VirtualizingWrapPanel created by S. Bäumlisberger licensed under MIT license.
https://github.com/sbaeumlisberger/VirtualizingWrapPanel
Copyright (C) S. Bäumlisberger
All Rights Reserved. */
using System.Windows.Controls;
using System.Windows.Data;
// ReSharper disable once CheckNamespace
namespace WPFluent.Controls;
///
/// Simple control that displays a gird of items. Depending on the orientation, the items are either stacked
/// horizontally or vertically until the items are wrapped to the next row or column. The control is using
/// virtualization to support large amount of items. In order to work properly all items must have the same
/// size. Based on .
///
public class VirtualizingGridView : System.Windows.Controls.ListView
{
///
/// Identifies the dependency property.
///
public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register(
nameof(Orientation),
typeof(Orientation),
typeof(VirtualizingGridView),
new PropertyMetadata(Orientation.Vertical));
///
/// Identifies the dependency property.
///
public static readonly DependencyProperty SpacingModeProperty = DependencyProperty.Register(
nameof(SpacingMode),
typeof(SpacingMode),
typeof(VirtualizingGridView),
new PropertyMetadata(SpacingMode.Uniform));
///
/// Identifies the dependency property.
///
public static readonly DependencyProperty StretchItemsProperty = DependencyProperty.Register(
nameof(StretchItems),
typeof(bool),
typeof(VirtualizingGridView),
new PropertyMetadata(false));
public VirtualizingGridView()
{
VirtualizingPanel.SetCacheLengthUnit(this, VirtualizationCacheLengthUnit.Page);
VirtualizingPanel.SetCacheLength(this, new VirtualizationCacheLength(1));
VirtualizingPanel.SetIsVirtualizingWhenGrouping(this, true);
}
///
/// Initializes the with .
///
protected virtual void InitializeItemsPanel()
{
var factory = new FrameworkElementFactory(typeof(VirtualizingWrapPanel));
factory.SetBinding(
VirtualizingWrapPanel.OrientationProperty,
new Binding { Source = this, Path = new PropertyPath(nameof(Orientation)), Mode = BindingMode.OneWay, });
factory.SetBinding(
VirtualizingWrapPanel.SpacingModeProperty,
new Binding { Source = this, Path = new PropertyPath(nameof(SpacingMode)), Mode = BindingMode.OneWay, });
factory.SetBinding(
VirtualizingWrapPanel.StretchItemsProperty,
new Binding { Source = this, Path = new PropertyPath(nameof(StretchItems)), Mode = BindingMode.OneWay, });
SetCurrentValue(ItemsPanelProperty, new ItemsPanelTemplate(factory));
}
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
InitializeItemsPanel();
}
///
/// Gets or sets a value that specifies the orientation in which items are arranged. The default value is .
///
public Orientation Orientation
{
get => (Orientation)GetValue(OrientationProperty);
set => SetValue(OrientationProperty, value);
}
///
/// Gets or sets the spacing mode used when arranging the items. The default value is .
///
public SpacingMode SpacingMode
{
get => (SpacingMode)GetValue(SpacingModeProperty);
set => SetValue(SpacingModeProperty, value);
}
///
/// Gets or sets a value indicating whether the items get stretched to fill up remaining space. The default value is
/// false.
///
///
/// The MaxWidth and MaxHeight properties of the ItemContainerStyle can be used to limit the stretching. In this
/// case the use of the remaining space will be determined by the SpacingMode property.
///
public bool StretchItems
{
get => (bool)GetValue(StretchItemsProperty);
set => SetValue(StretchItemsProperty, value);
}
}