使用 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 中设置数据绑定 我有班级人员 它通过一个文本框进行更新 类似老式的 另一个文本框应该通过数据绑定镜像对人员对象的更改 它曾经是 type twoway 但抛出了xamlparse 异常 它不是这样工作的 点击显示 pe
  • 传递给命令 WPF 的多个参数[重复]

    这个问题在这里已经有答案了 我有以下层次结构 abstract class TicketBase public DateTime PublishedDate get set class TicketTypeA TicketBase publ
  • MultiDataTrigger 使用 OR 而不是 AND

    我正在尝试设置多个DataTriggers on my Button 我做了一些研究发现MultiDataTrigger允许您执行此操作 我想要Visibility我的财产Button如果设置为 falseCCTVPath string E
  • 如何将 ObservableCollection 绑定到 AvalonDock DocumentPaneGroup?

    我需要在 AvalonDock 2 0 中加载项目集合作为文档 这些对象继承自一个抽象类 我想根据哪个子类在文档中渲染一个框架 这是我的 XAML
  • 转换BitmapImage后内存未释放

    我遇到以下 C 测试 代码的问题 public static void TestBitmap2ByteArray BitmapImage bitmap JpegBitmapEncoder encoder new JpegBitmapEnco
  • 使用 INotifyPropertyChanged

    有人可以解释一下为什么在 wpf 中使用绑定时需要使用 INotifyPropertyChanged 的 实现吗 我可以在不实现此接口的情况下绑定属性吗 例如我有代码 public class StudentData INotifyProp
  • 列表视图上的 TextBlock:如何忽略 TextBlock 中的点击并让列表视图处理它们

    我有一个显示大量信息的列表视图 但是当它为空时 我想在其上覆盖一个文本块 上面写着 没有要显示的信息 或 bla bla bla 添加信息 列表视图设置为响应鼠标单击 但现在如果我单击文本块 这些事件将路由到文本块 我怎样才能让这些事件转到
  • ItemSource 中具有不同类型数据的 ListView 多行列标题

    继续this https stackoverflow com q 26712051 1997232问题 我想实现这种ListView 它应该有两件事 多行列标题 不同的数据类型通过绑定ItemsSource以不同方式显示 为了解决 1 我尝
  • 窗口关闭后仍在调用方法

    首先我不知道这是不是一个愚蠢的问题 我有这样的场景 首先我有一个主窗口 public MainWindow InitializeComponent dt is a System Windows Threading DispatcherTim
  • 根据属性的类型使用文本框或复选框

    如果我有这样的结构 public class Parent public string Name get set public List
  • VisualStateManager 和生成的转换

    正当我以为我明白的时候VisualStateManager 有些事情证明我错了 我正在使用 WPF 4 并且尝试简单地在鼠标悬停时放大某个项目 并在鼠标离开时将其缩小 我想我只需定义每个状态VisualStateGroup然后指定一个Vis
  • ListView ItemContainerStyle 模板

    我创建了用作 ListView 项目模板的样式 其中包含 CheckBox 和 TextBlock
  • 如何使 WPF 应用程序在 Web 浏览器上运行

    我们有一个用 WPF 编写的现有应用程序 我们正在寻找一种迁移它的方法 以便它可以在 Web 浏览器上运行 深入研究后 我们似乎需要找到一种将代码编译为 WebAssembly 的方法 但我不确定它对于现有的完全加载的 WPF 应用程序有多
  • Wpf TextBlock 中的垂直文本

    是否可以垂直显示 TextBlock 中的文本 以便所有字母彼此堆叠 不使用 LayoutTransform 旋转 还没有人提到使用纯 XAML 垂直堆叠任意字符串的字母 不旋转它们 的明显而简单的方法
  • 给图像着色[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我正在尝试着色System Windows Controls Image 该图像包含透明区域 我只是想用颜色给非透明区域着色 例如 图
  • 如何在wpf中翻转图像

    我最近学习了如何使用 TransformedBitmap 和 RotateTransformed 类旋转 BitmapImage 现在我可以对图像进行顺时针旋转 但如何翻转图像呢 我找不到执行 BitmapImage 水平和垂直翻转的类 请
  • PreviewKeyDown 不会在 ElementHost 中针对 Tab 和箭头键触发

    我在 Winforms ElementHost 中有一个 WPF 窗口 我的窗口上的 Tab 键和箭头键不会触发 KeyDown 和 PreviewKeyDown 事件 KeyUp 和 PreviewKeyUp 似乎工作正常 Preview
  • WPF:通过拖放重新排序 WrapPanel 内容?

    我正在寻找一种通过拖放对 WPF WrapPanel 的内容 项目 重新排序的方法 我只想单击一个项目并将其拖动到新位置 据我了解 这是一项非常常见的任务 我想知道有人已经这样做了 或者知道如何实现此功能 我已经进行了谷歌搜索 但什么也没找
  • WPF 创建同级窗口并关闭当前窗口

    我需要的是我的窗口类中的这样一个事件处理程序 void someEventHandler object sender RoutedEventArgs e MyNewWindow mnw new MyNewWindow mnw Owner W
  • 解释 System.Diagnostics.CodeAnalysis.SuppressMessage

    我在某些应用程序中有这种代码 来自微软 assembly System Diagnostics CodeAnalysis SuppressMessage Microsoft Naming CA1702 CompoundWordsShould

随机推荐