Skip to content

Commit

Permalink
Merge pull request CommunityToolkit#2408 from skendrot/BladeViewItems…
Browse files Browse the repository at this point in the history
…Source

Update BladeView to allow binding of ItemsSource
  • Loading branch information
skendrot authored Oct 15, 2018
2 parents cc4eaaa + 5b15053 commit e29e20a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ private static void OnBladeModeChanged(DependencyObject dependencyObject, Depend

if (bladeView.Items != null)
{
foreach (BladeItem bladeItem in bladeView.Items)
foreach (var item in bladeView.Items)
{
var bladeItem = bladeView.GetBladeItem(item);
bladeView._cachedBladeItemSizes.Add(bladeItem, new Size(bladeItem.Width, bladeItem.Height));
}
}
Expand Down
60 changes: 34 additions & 26 deletions Microsoft.Toolkit.Uwp.UI.Controls/BladeView/BladeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,27 @@ public BladeView()
SizeChanged += (sender, e) => AdjustBladeItemSize();
}

/// <summary>
/// Override default OnApplyTemplate to capture child controls
/// </summary>
/// <inheritdoc/>
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
CycleBlades();
AdjustBladeItemSize();
}

/// <summary>
/// Creates or identifies the element that is used to display the given item.
/// </summary>
/// <returns>
/// The element that is used to display the given item.
/// </returns>
/// <inheritdoc/>
protected override DependencyObject GetContainerForItemOverride()
{
return new BladeItem();
}

/// <summary>
/// Prepares the specified element to display the specified item.
/// </summary>
/// <param name="element">The element that's used to display the specified item.</param>
/// <param name="item">The item to display.</param>
/// <inheritdoc/>
protected override bool IsItemItsOwnContainerOverride(object item)
{
return item is BladeItem;
}

/// <inheritdoc/>
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
var blade = element as BladeItem;
Expand All @@ -76,11 +71,7 @@ protected override void PrepareContainerForItemOverride(DependencyObject element
CycleBlades();
}

/// <summary>
/// Undoes the effects of the PrepareContainerForItemOverride method.
/// </summary>
/// <param name="element">The container element.</param>
/// <param name="item">The item.</param>
/// <inheritdoc/>
protected override void ClearContainerForItemOverride(DependencyObject element, object item)
{
var blade = element as BladeItem;
Expand All @@ -95,11 +86,15 @@ protected override void ClearContainerForItemOverride(DependencyObject element,
private void CycleBlades()
{
ActiveBlades = new ObservableCollection<BladeItem>();
foreach (var blade in Items.OfType<BladeItem>())
foreach (var item in Items)
{
if (blade.IsOpen)
BladeItem blade = GetBladeItem(item);
if (blade != null)
{
ActiveBlades.Add(blade);
if (blade.IsOpen)
{
ActiveBlades.Add(blade);
}
}
}

Expand All @@ -117,6 +112,17 @@ private void CycleBlades()
}
}

private BladeItem GetBladeItem(object item)
{
BladeItem blade = item as BladeItem;
if (blade == null)
{
blade = (BladeItem)ContainerFromItem(item);
}

return blade;
}

private async void BladeOnVisibilityChanged(object sender, Visibility visibility)
{
var blade = sender as BladeItem;
Expand All @@ -128,8 +134,9 @@ private async void BladeOnVisibilityChanged(object sender, Visibility visibility
return;
}

Items.Remove(blade);
Items.Add(blade);
var item = ItemFromContainer(blade);
Items.Remove(item);
Items.Add(item);
BladeOpened?.Invoke(this, blade);
ActiveBlades.Add(blade);
UpdateLayout();
Expand Down Expand Up @@ -163,8 +170,9 @@ private void AdjustBladeItemSize()
// Adjust blade items to be full screen
if (BladeMode == BladeMode.Fullscreen && GetScrollViewer() != null)
{
foreach (BladeItem blade in Items)
foreach (var item in Items)
{
var blade = GetBladeItem(item);
blade.Width = _scrollViewer.ActualWidth;
blade.Height = _scrollViewer.ActualHeight;
}
Expand All @@ -175,7 +183,7 @@ private void ItemsVectorChanged(IObservableVector<object> sender, IVectorChanged
{
if (BladeMode == BladeMode.Fullscreen)
{
var bladeItem = (BladeItem)sender[(int)e.Index];
var bladeItem = GetBladeItem(sender[(int)e.Index]);
if (bladeItem != null)
{
if (!_cachedBladeItemSizes.ContainsKey(bladeItem))
Expand Down

0 comments on commit e29e20a

Please sign in to comment.