c#uwp GridView自定义面板

时间:2016-01-13 00:22:23

标签: c# windows-10 uwp

我仍在使用我的GridView自定义面板(请参阅其他问题:UWP c# Binding to ItemsPanel / Custom Panel (Template)

我已经使用自定义面板设置了GridView。现在,如果我将GridView选择模式从“多个”切换为“单个”,反之亦然,则自定义面板中的项目不会自动更新=> “选择方块”不会同时显示/隐藏所有项目,只有当我将鼠标放在它上面时才会显示/隐藏。

如果我使用ItemsWrapGrid,则不会发生这种情况......我需要在自定义面板中实现哪种事件/方法?

到目前为止我的小组代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using teachers_cal;
using teachers_cal.Classes;

namespace teachers_cal.UserControls
{
public class PRGD010_GridViewPanel : Panel
{
    public int NumberRowsOrColumns
    {
        get { return (int)GetValue(NumberRowsOrColumnsProperty); }
        set { SetValue(NumberRowsOrColumnsProperty, value < 1 ? 0 : value); }
    }

    public static readonly DependencyProperty NumberRowsOrColumnsProperty = DependencyProperty.Register("NumberRowsOrColumns", typeof(int), typeof(PRGD010_GridViewPanel), new PropertyMetadata(1, OnNumberRowsOrColumnsPropertyChanged));
    private static void OnNumberRowsOrColumnsPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
    {
        (source as PRGD010_GridViewPanel).InvalidateMeasure();
    }

    public int Offset
    {
        get { return (int)GetValue(StartPositionProperty); }
        set { SetValue(StartPositionProperty, value >= this.NumberRowsOrColumns ? this.NumberRowsOrColumns - 1 : value); }
    }

    public static readonly DependencyProperty StartPositionProperty = DependencyProperty.Register("Offset", typeof(int), typeof(PRGD010_GridViewPanel), new PropertyMetadata(0, OnStartPositionPropertyChanged));
    private static void OnStartPositionPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
    {
        (source as PRGD010_GridViewPanel).InvalidateMeasure();
    }

    public PRGD010_GridViewPanel()
    {
    }

    /// Get Items Source count items
    private int GetItemsCount()
    {
        return this.Children != null ? this.Children.Count : 0;
    }

    /// Get Calculated Row or Column count. Depending on Offset, Numbers and Items
    private int GetRowsOrColumnsCount()
    {
        int additionalLine = ((GetItemsCount() + this.Offset)) % this.NumberRowsOrColumns > 0 ? 1 : 0;
        return ((GetItemsCount() + this.Offset)) / this.NumberRowsOrColumns + additionalLine;
    }


    /// <summary>
    /// Arrange all items
    /// </summary>
    protected override Size ArrangeOverride(Size finalSize)
    {

        // Clip to ensure items dont override container
        this.Clip = new RectangleGeometry { Rect = new Rect(0, 0, finalSize.Width, finalSize.Height) };

        Double positionTop = 0d;
        double positionGrid = this.Offset;
        double test = this.NumberRowsOrColumns;

        // Must Create looping items count
        foreach (UIElement item in this.Children)
        {
            if (item == null)
                continue;

            Size desiredSize = item.DesiredSize;

            if (double.IsNaN(desiredSize.Width) || double.IsNaN(desiredSize.Height)) continue;

            // Get rect position
            var rect = new Rect(positionGrid * desiredSize.Width, positionTop, desiredSize.Width, desiredSize.Height);
            item.Arrange(rect);

            // set internal CompositeTransform to handle movement
            TranslateTransform compositeTransform = new TranslateTransform();
            item.RenderTransform = compositeTransform;

            if (positionGrid < this.NumberRowsOrColumns - 1) { positionGrid += 1; }
            else
            {
                positionGrid = 0;
                positionTop += desiredSize.Height;
            }
        }

        return finalSize;
    }

    /// <summary>
    /// Measure items 
    /// </summary>
    protected override Size MeasureOverride(Size availableSize)
    {

        //Size s = base.MeasureOverride(availableSize);

        // set good cliping
        //this.Clip = new RectangleGeometry { Rect = new Rect(0, 0, s.Width, s.Height) };

        // Measure all items
        foreach (UIElement container in this.Children)
        {
            container.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
        }

        double width = Children[0]?.DesiredSize.Width ?? 0;
        double height = Children[0]?.DesiredSize.Height ?? 0;
        var i = GetRowsOrColumnsCount();

        return (new Size(width * (double)NumberRowsOrColumns, height * (double)GetRowsOrColumnsCount()));
        //return (new Size(3000, 3000));
    }
}
}

编辑: 我创建了两个简单的项目。即使使用标准的ItemPanels,我也有奇怪的行为。这真是令人沮丧

  1. 将GridView与VariableSizeWrapGrid一起使用时,当我更改SelectionMode时,GridViewItems将无法正确刷新

  2. 对ItemsWrapGrip执行相同操作是可以的。更改SelectionMode将正确显示...

  3. 有人能弄清楚为什么会这样吗?怎么办?!

    见两个链接......

    https://onedrive.live.com/redir?resid=4305F58623B1701D!42317&authkey=!APnPBY_9SNDS4AI&ithint=file%2czip

    https://onedrive.live.com/redir?resid=4305F58623B1701D!42316&authkey=!AMDZtSBQ5bjWaoU&ithint=file%2czip

    感谢您的回复

0 个答案:

没有答案