使用 SharedSizeGroup 测量/排列网格

2024-04-21

两个包含以某种方式指定的元素的网格和 SharedSizeGroup 似乎存在一些问题。

这个问题是为了回答先前的问题 https://stackoverflow.com/questions/3865033/grid-height-not-adjusting-properly-when-a-row-height-with-sharedsizegroup-changes/3865406#3865406 from 用户 D.H. https://stackoverflow.com/users/310042/d-h我试图回答。请原谅它的长度,但它有助于直观地展示问题。

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

事实证明,它确实与“测量和安排”有关。其实这和not进行一项措施。我觉得这可能至少是一个问题,如果不是一个错误,但希望对此行为有一个解释。

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

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

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

减少右侧 TextBlock 但仍大于左侧 TextBlock
当右侧网格的大小减小,但仍然大于左侧硬编码 TextBlock 的大小时,您可以看到,由于 SharedSizeGroup,两个网格再次调整到新的高度。右侧的列反映了网格的测量和排列调用。

将右侧 TextBlock 的大小减小到小于左侧 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>

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

公共窗口1() { 初始化组件();

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

据微软称 http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/609ff234-1bc8-4242-81bf-1a1baa5bc45b/,这是一个错误。

这似乎是 WPF 中的一个错误,并且 微软已经意识到这一点并且 研究解决方案。

如果您需要解决方法方面的帮助, 请联系 Microsoft 支持:

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

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

http://connect.microsoft.com/VisualStudio http://connect.microsoft.com/VisualStudio

我已将此作为错误提交连接站点 https://connect.microsoft.com/VisualStudio/feedback/details/613904/measure-arrange-of-grids-with-sharedsizegroup.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 SharedSizeGroup 测量/排列网格 的相关文章

  • WPF 网格布局

    是否可以在 WPF 中使用 Grid 来设计类似的东西 设计列很容易 但是行呢 或者有没有更好的解决方案 比如另一个容器 将每个矩形想象为模块 GroupBox 创建一个包含两列的外部网格 在此网格中 放置另外两个网格 每列一个 这将导致所
  • 如何按 z-index 对 Windows 进行排序?

    如果我枚举窗口Application Current Windows 对于任意两个窗口 我如何判断哪一个 更接近 即具有更大的 z index 或者 换句话说 我如何按 z 索引对这些窗口进行排序 您无法从 WPF 获取 Window 的
  • WPF 窗口关闭后不会释放内存

    我创建了一个测试代码 private void Application Startup 1 object sender StartupEventArgs e ShutdownMode System Windows ShutdownMode
  • 弹簧质量系统的阻尼效果(或者这是 ElasticEase?)

    我试图在代码中模拟动画效果 几乎任何语言都可以 因为它看起来是数学而不是语言 本质上 它是质量弹簧系统的仿真 我一直在研究 WPF Silverlight 的ElasticEase这似乎与我正在寻找的非常接近 但又不完全一样 首先 这就是我
  • WPF ComboBox 中具有本地化名称的枚举

    我有一个列出枚举的组合框 enum StatusEnum Open 1 Closed 2 InProgress 3
  • WPF MVVM 在窗口关闭时调用 ViewModel Save 方法

    我已经弄清楚如何从我的 ViewModel 关闭窗口 现在我需要从另一侧解决窗口关闭问题 当用户单击窗口的关闭按钮时 我需要在 ViewModel 中触发 Save 方法 我正在考虑将 Command 属性绑定到 Window 的关闭事件
  • 背景颜色变化

    SolidColorBrush bgColor public ModernBTN InitializeComponent this Loaded delegate object sender RoutedEventArgs e bgColo
  • 如何制作wpf倒计时器?

    我想创建 wpf 倒数计时器 将结果显示为hh mm ss进入文本框 我将感谢任何人的帮助 您可以使用DispatcherTimer class msdn http msdn microsoft com en US library syst
  • 如何在鼠标悬停时突出显示 MahApps.Metro 的图块?

    我刚刚发现了一个非常出色的 WPF UI 工具包 MahApps Metro 我创建了一个带有 MahApps Metro Controls Tile 类型的图块的窗口 当鼠标经过时 我无法找到突出显示图块的正确方法 你知道我该怎么做吗 我
  • OxyPlot 中日期时间轴上的不规则间隔

    我有一个 WPF 应用程序 其中使用 OxyPlot 来绘制图表 我不断地将点添加到图表中的线系列中 X 轴是日期时间轴 其间隔类型设置为秒 点不断添加到线系列中 当第一个点和最后一个点之间的时间跨度是特定的秒数时 我删除第一个点并使绘图无
  • 当绑定值为 null 时出现 WPF 日期选择器验证错误

    我有一个 WPF 应用程序 其中使用绑定到实体框架 带有 SQL Server 实体的日期字段的日期选择器 我将其绑定如下
  • WPF:在 DependencyProperty.UnsetValue 上触发

    在回答中这个问题 https stackoverflow com questions 2596847 wpf applying a trigger on binding failure作出以下声明 理论上 可能 触发 x Static De
  • WPF TabControl的SelectedIndex设置问题

    我有一个包含两个项目的 TabControl
  • 如何从 XAML 中为样式中的控件指定工具提示?

    我正在使用 Microsoft CodePlex 项目中的 WPF 数据网格 我有一个自定义控件 我想将其数据绑定到数据网格行中的字段 我一生都无法弄清楚如何在数据网格行上指定工具提示 我最接近的是使用 RowStyle 和 Setter
  • WPF MVVM将DataTable绑定到DataGrid不显示数据

    我有一个简单的控件 其中包含一个 DataGrid 其中 ItemsSource 绑定到 DataTable 当我填充 DataTable 时 我可以看到 DataGrid 中添加了行 但没有显示任何数据 我没有为此 DataGrid 使用
  • 在 WPF 树视图中获取 FullPath?

    如果我以编程方式创建 WPF TreeView 例如 TreeView treeView lt added in the designer TreeViewItem rootNode new TreeViewItem rootNode He
  • 默认转换器何时启动?

    使用以下代码 虽然 Text 属性绑定到 DateTime 源属性 但我注意到 WPF 似乎会自动将文本转换为 DateTime 而无需我编写 ValueConverter 有人可以解释一下这是如何完成的吗
  • 消息框按钮文本

    我环顾四周 似乎所有内容都可以在消息框中编辑 禁止按钮上的文本 WPF http en wikipedia org wiki Windows Presentation Foundation C MessageBox Show Generic
  • wpf 的 prism 与 mvvm light

    我们正在启动一个带有 MVVM 的 WPF 项目 并且必须决定使用 PRISM 还是 MVVM Light 我对这两个框架都是新手 我已经阅读了一些帖子 但仍然有一些问题 有人可以阐明以下几个方面吗 两个框架 性能 无论出于何种原因 其中一
  • 了解使用 Windows 本机 WPF 客户端进行 ADFS 登录

    我已经阅读了大量有关 ADFS 与 NodeJS Angular 或其他前端 Web 框架集成以及一般流程如何工作的文献 并通过 Auth0 Angular 起始代码构建了概念证明 但我不明白如何这可以与本机 WPF Windows 应用程

随机推荐