質問

特定の方法で指定された要素を含む2つのグリッドとSharedSizeGroupには、少し問題があるようです。

この質問はanに応じています 以前の質問 から ユーザーDH 私は答えようとしました。長さを許しますが、問題を視覚的に実証するのに役立ちます。

彼の元の質問は、特定の条件が満たされたときに、共有サイズグループを持つ2つのグリッドが同じ高さにサイズを変更しなかった理由を尋ねました(右側のグリッドでテキストブロックをサイズ変更します)。私は彼の模範を取り、それを拡張しました。なぜなら、それはメジャー/アレンジサイクルに関係しているのではないかと疑ったからです。

実際、測定と配置に関係している必要があることがわかります。実際、それは関係しています いいえ 測定を行う。 これは、バグではないにしても、少なくとも問題になるかもしれませんが、行動の説明をしたいと思います。

何が起こるかの簡単な概要を次に示します(デモのみのために色の派手な色)。

起動
どちらのグリッドにも3つの行があり、それぞれにテキストブロックが含まれています。中央の列はSharedSizeGroupのものです。中央の列のテキストは、テキストブロックの実際の高さに縛られており、初期の高さプロパティは表示されている値にハードコードされています。グリッドの下の数字は、そのグリッドの実際のものを表しています。左グリッドの背景色は緑色であることに注意してください。

Startup

右側のテキストブロックを増やします
右側のグリッドのサイズが大きくなると、共有サイズグループのため、両方のグリッドが新しい高さにサイズを変更することがわかります。右側の列は、測定値を反映し、グリッドの呼び出しを手配します。

Increased In Size

右側のテキストブロックを減らすが、左側のテキストブロックよりも大きい
右側のグリッドのサイズが減少しているが、左側のハードコーディングされたテキストブロックのサイズよりも大きい場合、SharedSizeGroupにより、両方のグリッドが再び新しい高さにサイズを変更することがわかります。右側の列は、測定値を反映し、グリッドの呼び出しを手配します。

Decreased to Minimum Size

左側のテキストブロックのサイズよりも少ない右側のテキストブロックを減らす
右側のグリッドのサイズが減少し、左側のハードコーディングされたテキストブロックのサイズよりも少ない場合、左側のグリッドが「適切な」サイズに減少しないことがわかります。下部のグリッドの緑の背景と、グリッドのサイズが150であり、130ではなく150であるという事実。

右側の情報を見ると、左グリッドが手配したことに気付くでしょうが、 測定をしませんでした。

Decreased Past Size


問題を複製するための例コードを次に示します。

InfogridおよびInfogrideventargsクラス

using System.Windows;
using System.Windows.Controls;
namespace GridMeasureExample
{
    class InfoGrid : Grid
    {
        protected override Size ArrangeOverride(Size arrangeSize)
        {
            CallReportInfoEvent("Arrange");
            return base.ArrangeOverride(arrangeSize);
        }
        protected override Size MeasureOverride(Size constraint)
        {
            CallReportInfoEvent("Measure");
            return base.MeasureOverride(constraint);
        }
        public event EventHandler<InfoGridEventArgs> ReportInfo;
        private void CallReportInfoEvent(string message)
        {
            if (ReportInfo != null)
                ReportInfo(this, new InfoGridEventArgs(message));
        }
    }
    public class InfoGridEventArgs : EventArgs
    {
        private InfoGridEventArgs()
        {
        }
        public InfoGridEventArgs(string message)
        {
            this.TimeStamp = DateTime.Now;
            this.Message = message;
        }
        public DateTime TimeStamp
        {
            get;
            private set;
        }
        public String Message
        {
            get;
            private set;
        }
    }
}

メインウィンドウXAML

<Window x:Class="GridMeasureExample.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:GridMeasureExample"
        Title="SharedSizeGroup" Height="500" Width="500">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <StackPanel Grid.Column="0" 
                    Grid.Row="0"
                    Orientation="Horizontal" 
                    HorizontalAlignment="Left"
                    VerticalAlignment="Top"
                    Grid.IsSharedSizeScope="True">

            <StackPanel Orientation="Vertical" Width="100">
                <local:InfoGrid x:Name="grid1" Background="Green" ShowGridLines="True">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="15" />
                        <RowDefinition SharedSizeGroup="Group1" />
                        <RowDefinition Height="15" />
                    </Grid.RowDefinitions>
                    <TextBlock Background="Blue" Grid.Row="0" Text="Row 0"/>
                    <TextBlock Background="Red" Grid.Row="1" Name="textBlock1" Height="100"
                           Text="{Binding RelativeSource={RelativeSource Self}, Path=ActualHeight}"/>
                    <TextBlock Background="Blue" Grid.Row="2" Text="Row 2" />
                </local:InfoGrid>
                <TextBlock Text="{Binding Path=ActualHeight, ElementName=grid1}" />
            </StackPanel>

            <StackPanel Orientation="Vertical" Width="100">
                <local:InfoGrid x:Name="grid2" Background="Yellow" ShowGridLines="True">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="15" />
                        <RowDefinition SharedSizeGroup="Group1" />
                        <RowDefinition Height="15" />
                    </Grid.RowDefinitions>
                    <TextBlock Background="Orange" Grid.Row="0" Text="Row 0" />
                    <TextBlock Background="Purple" Grid.Row="1" Name="textBlock2" Height="150"
                           Text="{Binding RelativeSource={RelativeSource Self}, Path=ActualHeight}"/>
                    <TextBlock Background="Orange" Grid.Row="2" Text="Row 2" />
                </local:InfoGrid>
                <TextBlock Text="{Binding Path=ActualHeight, ElementName=grid2}" />
            </StackPanel>

        </StackPanel>

        <ListBox x:Name="lstInfo"
                 Grid.Column="1"
                 Grid.Row="0"
                 Margin="10,0,0,0"
                 HorizontalAlignment="Stretch"
                 VerticalAlignment="Stretch" />

        <UniformGrid Grid.Column="0"
                     Grid.Row="1"
                     Grid.ColumnSpan="2"
                     Columns="2"
                     HorizontalAlignment="Center"
                     Margin="5">
            <Button x:Name="btnIncrease" Margin="4,0">Increase</Button>
            <Button x:Name="btnDecrease" Margin="4,0">Decrease</Button>
        </UniformGrid>

    </Grid>

</Window>

メインウィンドウコンストラクター(Code-Behindのコードのみ)

public window1(){initializeComponent();

    btnIncrease.Click += (s, e) => 
        {
            lstInfo.Items.Add(String.Format("{0} Increase Button Pressed", DateTime.Now.ToString("HH:mm:ss.ffff")));
            textBlock2.Height += 30;
        };
    btnDecrease.Click += (s, e) =>
        {
            lstInfo.Items.Add(String.Format("{0} Decrease Button Pressed", DateTime.Now.ToString("HH:mm:ss.ffff")));
            if (textBlock2.ActualHeight >= 30)
                textBlock2.Height -= 30;
        };

    grid1.ReportInfo += (s, e) => lstInfo.Items.Add(String.Format("{0} Left Grid: {1}", e.TimeStamp.ToString("HH:mm:ss.ffff"), e.Message));
    grid2.ReportInfo += (s, e) => lstInfo.Items.Add(String.Format("{0} Right Grid: {1}", e.TimeStamp.ToString("HH:mm:ss.ffff"), e.Message));
}
役に立ちましたか?

解決

Microsoftによると, 、それはバグです。

これはWPFのバグのようであり、Microsoftはそれを認識し、解決策を調査しています。

回避策のサポートが必要な場合は、Microsoftサポートにお問い合わせください

http://support.microsoft.com/default.aspx?id=fh; en-us; offerprophone

また、この問題に関するWPFのバグフィードバックを…で送信することもできます。

http://connect.microsoft.com/visualstudio

これをバグとして提出しました 接続サイト.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top