2

カードの手札を表すカスタムパネルをコーディングしています。カードを横に積み重ねるパネルです。十分なスペースがない場合、各カードはその左側のカードの一部と重なります。最小部分は常に表示されている必要があります。私はこれを達成しました、そしてこれはコードです:

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で作成できますか?そうでない場合は、別のアイデアをいただければ幸いです。ypurの助けをありがとうございました。

ジュリカ

4

1 に答える 1

2

まず、パネルのロジックを正反対に変更します。MeasureOverrideカードをできるだけしっかりと詰めてから、ArrangeOverride与えられた幅に均等に広げます。

次に、MinWidthプロパティを使用します。にバインドしScrollViewer.ActualWidthます。

このように、カードをの幅よりも狭い幅にしっかりと詰めることができる場合は、ScrollViewer使用Hand可能なすべてのスペースに引き伸ばされます。そして、それができない場合は、Hand'の幅は計算したとおりになります。

于 2010-10-20T20:40:31.593 に答える