自定义面板中自定义滚动的问题

时间:2010-10-20 14:18:21

标签: wpf scrollviewer custom-panel

我正在编写代表牌手的自定义面板。它是一个可以水平堆叠卡片的面板。如果没有足够的空间,每张卡将与其左侧卡片的一部分重叠。最小部分应始终可见。我完成了这个,这是代码:

using System;
using System.Windows;
using System.Windows.Controls;

namespace Hand
{
   public class Hand : Panel
   {
      //TODO Should be dependancy property
      private const double MIN_PART = 0.5;

      protected override Size MeasureOverride(Size availableSize)
      {
         Size desiredSize = new Size();
         foreach (UIElement element in this.Children)
         {
            element.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));

            desiredSize.Width += element.DesiredSize.Width;
            desiredSize.Height = Math.Max(desiredSize.Height, element.DesiredSize.Height);
         }
         return desiredSize;
      }

      protected override Size ArrangeOverride(Size finalSize)
      {
         //percentage of the visible part of the child.
         double part = 1;

         Double desiredWidth = 0;

         //TODO Check how to get desired size because without looping
         //this.DesiredSize is minimum of available size and size returned from MeasureOverride
         foreach (UIElement element in this.Children)
         {
            desiredWidth += element.DesiredSize.Width;
         }

         if (desiredWidth > this.DesiredSize.Width)
         {
            //Every, but the last child should be overlapped
            double lastChildWidth = this.Children[this.Children.Count - 1].DesiredSize.Width;
            part = (this.DesiredSize.Width - lastChildWidth) / (desiredWidth - lastChildWidth);

            part = Math.Max(part, MIN_PART);
         }

         double x = 0;

         foreach (UIElement element in this.Children)
         {
            Rect rect = new Rect(x, 0, element.DesiredSize.Width, element.DesiredSize.Height);
            element.Arrange(rect);
            finalSize.Width = x + element.DesiredSize.Width;
            x += element.DesiredSize.Width * part;
         }

         return finalSize;
      }
   }
}

我想在达到最小部分时添加滚动条,这样用户仍然可以查看所有卡片。我无法做到这一点。我尝试使用ScrollViewer:

<Window x:Class="TestScrollPanel.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:h="clr-namespace:Hand;assembly=Hand"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ScrollViewer HorizontalScrollBarVisibility="Auto">
        <h:Hand>
            <Button Width="100">One</Button>
            <Button Width="150">Two</Button>
            <Button Width="200">Three</Button>
        </h:Hand>
        </ScrollViewer>
    </Grid>
</Window>

但这不起作用,因为一旦水平滚动条可见,Hand面板的MeasureOveride和ArrangeOverride永远不会被调用,即使它被调用,Hand也会得到所需的大小来排列所有孩子而不重叠。

这可以用ScrollViewer完成,如果没有,可以欣赏另一个想法。 谢谢大家的帮助。

尤里察

1 个答案:

答案 0 :(得分:2)

首先,将面板的逻辑改为恰恰相反:让MeasureOverride尽可能紧密地包装卡片,然后让ArrangeOverride将它们均匀分布在任何宽度上。

其次,使用MinWidth属性。将其绑定到ScrollViewer.ActualWidth

这样,如果卡片的宽度小于ScrollViewer的宽度,那么您的Hand将被拉伸到所有可用空间。如果他们不能,那么Hand的宽度就是你计算它的任何东西。

相关问题