4

两个包含以某种方式指定的元素的网格和 SharedSizeGroup 似乎有点问题。

这个问题是对我试图回答 的用户 DH的早期问题的回应。原谅长度,但它有助于直观地展示问题。

他最初的问题是为什么两个具有 SharedSizeGroup 的网格在满足某些条件时没有调整到相同的高度(在右侧网格中调整 TextBlock 的大小)。我拿了他的例子并扩展了它,因为我怀疑它与测量/排列周期有关。

事实证明,它确实与 Measure and Arrange 有关。实际上,这与进行测量有关。 我觉得这可能至少是一个问题,如果不是一个错误,但想对这种行为进行解释。

以下是所发生情况的快速概述(花哨的颜色仅用于演示目的)。

启动
两个网格都有三行,每行包含一个 TextBlock。中间一行是 SharedSizeGroup。中间行的文本绑定到其 TextBlock 的 ActualHeight,初始 Height 属性硬编码为您看到的值。网格下方的数字代表该网格的实际高度。请注意,左侧网格的背景颜色为绿色。

启动

增加右侧文本
块 当右侧网格的大小增加时,您可以看到两个网格都调整到新的高度,这是由于 SharedSizeGroup。右侧的列反映了网格的 Measure 和 Arrange 调用。

尺寸增加

减小Right-Side TextBlock 但仍大于Left-Side TextBlock
当右侧网格尺寸减小,但仍大于左侧硬编码TextBlock 的大小时,可以看到两个网格再次调整大小由于 SharedSizeGroup,达到了新的高度。右侧的列反映了网格的 Measure 和 Arrange 调用。

减小到最小尺寸

减小 Right-Side TextBlock 的大小小于 Left-Side TextBlock
的大小 当右侧网格减小大小时,小于左侧硬编码 TextBlock 的大小,可以看到左侧网格确实没有减小到“适当的”大小,正如看到底部网格的绿色背景以及网格大小是 150,而不是 130 的事实所证明的那样。

如果你看右边的信息,你会注意到左边的网格做了一个排列,但没有做一个测量。

减少过去的大小


这是复制问题的示例代码。

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>

主窗口构造函数(仅代码隐藏代码)

公共 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));
}
4

1 回答 1

3

根据 Microsoft的说法,这是一个错误。

这似乎是 WPF 中的一个错误,Microsoft 已意识到这一点并正在调查解决方案。

如果您需要解决方法方面的帮助,请通过以下方式联系 Microsoft 支持

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

您也可以提交关于此问题的 WPF 错误反馈…</p>

http://connect.microsoft.com/VisualStudio

我已在Connect 站点上将此作为错误提交。

于 2010-10-15T03:04:36.647 回答