使用SharedSizeGroup度量/排列网格 [英] Measure/Arrange Of Grids with SharedSizeGroup

查看:88
本文介绍了使用SharedSizeGroup度量/排列网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

There seems to be a bit of an issue with two grids containing elements specified in a certain way, and the SharedSizeGroup.

此问题是对用户DH 的先前问题,我试图回答.原谅的长度,但有助于视觉上显示问题.

This question is in response to an earlier question from user D.H. I attempted to answer. Forgive the length, but it helps to demonstrate the problem visually.

他最初的问题是,为什么在满足某些条件时(具有右侧网格的TextBlock的大小),具有SharedSizeGroup的两个网格为何没有调整为相同的高度.我以他的示例为例,并进行了扩展,因为我怀疑它与Measure/Arrange循环有关.

His original question asked why two grids with a SharedSizeGroup didn't resize to the same height when certain conditions were met (resizing a TextBlock in the right-side grid). I took his example and expanded it, because I suspected that it had to do with the Measure/Arrange cycle.

事实证明,它实际上与Measure and Arrange有关.实际上,这与不是进行测量有关. 我认为这可能至少是一个问题,即使不是错误,也希望对此行为做出解释.

It turns out that it does, in fact, have to do with Measure and Arrange. Actually, it has to do with not doing a Measure. I feel that this may be at least an issue, if not a bug, but would like to have an explanation of the behavior.

以下是发生的情况的简要概述(仅用于演示目的的鲜艳颜色).

启动
两个网格都有三行,每行包含一个TextBlock.中间行是SharedSizeGroup.中间行的文本绑定到其TextBlock的ActualHeight,并且初始Height属性被硬编码为您看到的值.网格下方的数字表示该网格的ActualHeight.请注意,左侧网格的BackgroundColor为绿色.

Here is a quick overview of what happens (garish colors for demo purposes only).

Start Up
Both grids have three rows, each containing a TextBlock. The middle row is of SharedSizeGroup. The text of the middle row is bound to the ActualHeight of its TextBlock, with the initial Height property hard-coded to the values you see. The numbers below the grids represent the ActualHeight of that grid. Note that the BackgroundColor of the left grid is Green.

增加右侧TextBlock
当右侧网格的大小增加时,由于SharedSizeGroup,您可以看到两个网格的大小都重新调整为新的高度.右边的列反映了网格的Measure和Arrange调用.

Increasing the Right-Side TextBlock
When the right-side grid is increased in size, you can see that both grids resize to the new height, due to the SharedSizeGroup. The column at the right reflects the Measure and Arrange calls of the grids.

减小右侧TextBlock,但仍大于左侧TextBlock
当右侧网格的大小减小但仍大于左侧硬编码的TextBlock的大小时,由于SharedSizeGroup,您可以看到两个网格都再次调整为新的高度.右边的列反映了网格的Measure和Arrange调用.

Decreasing the Right-Side TextBlock But Still Greater Than Left-Side TextBlock
When the right-side grid is decreased in size, but still larger than the size of the hard-coded TextBlock of the left side, you can see that both grids again resize to the new height, due to the SharedSizeGroup. The column at the right reflects the Measure and Arrange calls of the grids.

将右侧TextBlock的大小减小到小于左侧TextBlock的大小
当右侧网格的大小减小到小于左侧硬编码的TextBlock的大小时,您可以看到左侧网格没有减小到适当"的大小,这可以通过查看网格的绿色背景位于底部,并且网格的大小为150,而不是130.

Decreasing the Right-Side TextBlock Less Than Size of Left-Side TextBlock
When the right-side grid is decreased in size, less than the size of the hard-coded TextBlock of the left side, you can see that the left-side grid does not decrease to the "proper" size, as evidenced by seeing the Green background of the grid at the bottom, and the fact that the size of the grid is 150, not 130.

如果您查看右侧的信息,您会注意到左"网格进行了排列",但未进行测量.

If you look at the info on the right, you will notice that the Left grid did an Arrange, but did not do a Measure.

这是重复此问题的示例代码.

Here is the example code to duplicate the issue.

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();

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));
}

推荐答案

这似乎是WPF中的错误,并且 Microsoft意识到了这一点,并且 研究解决方案.

This appears to be a bug in WPF, and Microsoft is aware of it and investigating a solution.

如果您需要有关解决方法的帮助, 请通过以下网址联系Microsoft支持:

If you need help with a workaround, please contact Microsoft Support at

http://support.microsoft.com/default. aspx?id = fh; zh-CN; offproprophone

您还可以提交有关的错误反馈 WPF关于此问题的信息……

You can also submit bug feedback for WPF regarding this issue at…

http://connect.microsoft.com/VisualStudio

我已将此错误提交为连接站点.

这篇关于使用SharedSizeGroup度量/排列网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆