2

私は UserControl を持っており、その UserControl は縦横比でサイズ変更する必要があります。つまり、幅:高さ = 2:1 です。現在、私はこのコードを使用しています:

    protected override Size ArrangeOverride(Size arrangeBounds)
    {
        if (ActualWidth == 0 || ActualHeight == 0) return arrangeBounds;
        base.ArrangeOverride(arrangeBounds);
        double ratio = 2;

        if (Parent != null)
        {
            var size = new Size(arrangeBounds.Height * ratio, arrangeBounds.Height);

            double containerWidth = ((FrameworkElement)Parent).ActualWidth;
            if (containerWidth < size.Width)
            {
                double newHeight = arrangeBounds.Height * (containerWidth / size.Width);
                canvas.Width = newHeight * ratio;
                canvas.Height = newHeight;
            }
            else
            {
                canvas.Width = size.Height * ratio;
                canvas.Height = size.Height;
            }
        }

        return arrangeBounds;
    }

しかし、実際には機能していません。つまり、機能しますが、毎回ではありません。私が最大の場合。ウィンドウのサイズが変更されないことがあるため、コントロールのサイズが変更された場合は少し「ランダム」です。したがって、誰かがより良い解決策を持っているとしたら、それはとてもいいことです。

4

3 に答える 3

5

最も簡単な解決策は、値コンバーターを介して、高さを幅に直接バインドすることです。

于 2012-05-18T18:23:00.377 に答える
4

少し遅れましたが、最近同じ問題に遭遇しました。良い解決策が見つからなかったため、独自のレイアウト コントロール/デコレータを作成することにし、それについてのブログ投稿をここに書きました。

http://coding4life.wordpress.com/2012/10/15/wpf-resize-maintain-aspect-ratio/

基本的に私の解決策は、 と の両方を上書きすることでしMeasureOverrideArrangeOverride。これまでのところ、すべての一般的なコンテナで非常にうまく機能し、あなたが説明したような問題は発生しませんでした.

機能するデコレータ コントロールを見つける投稿を読むことをお勧めしますが、最も重要な方法は次のとおりです。

   protected override Size MeasureOverride(Size constraint)
   {
      if (Child != null)
      {
         constraint = SizeToRatio(constraint, false);
         Child.Measure(constraint);

         if(double.IsInfinity(constraint.Width)
            || double.IsInfinity(constraint.Height))
         {
            return SizeToRatio(Child.DesiredSize, true);
         }

         return constraint;
      }

      // we don't have a child, so we don't need any space
      return new Size(0, 0);
   }

   protected override Size ArrangeOverride(Size arrangeSize)
   {
      if (Child != null)
      {
         var newSize = SizeToRatio(arrangeSize, false);

         double widthDelta = arrangeSize.Width - newSize.Width;
         double heightDelta = arrangeSize.Height - newSize.Height;

         double top = 0;
         double left = 0;

         if (!double.IsNaN(widthDelta)
            && !double.IsInfinity(widthDelta))
         {
            left = widthDelta/2;
         }

         if (!double.IsNaN(heightDelta)
            && !double.IsInfinity(heightDelta))
         {
            top = heightDelta/2;
         }

         var finalRect = new Rect(new Point(left, top), newSize);
         Child.Arrange(finalRect);
      }

      return arrangeSize;
   }

   public Size SizeToRatio(Size size, bool expand)
   {
      double ratio = AspectRatio;

      double height = size.Width / ratio;
      double width = size.Height * ratio;

      if (expand)
      {
         width = Math.Max(width, size.Width);
         height = Math.Max(height, size.Height);
      }
      else
      {
         width = Math.Min(width, size.Width);
         height = Math.Min(height, size.Height);
      }

      return new Size(width, height);
   }

誰かの役に立てば幸いです!

于 2012-10-15T22:06:49.677 に答える
3

コントロールを でラップし、ViewBoxを に設定ViewBox.StretchUniformます。そのように MaxWidth と MaxHeight を制限することもできます。

Stretch-Enumeration @ MSDN の詳細

Stretch @ MSDN の適用方法

于 2012-05-23T06:32:20.257 に答える