Высота сетки не корректируется при изменении высоты строки с SharedSizeGroup - PullRequest
1 голос
/ 05 октября 2010

У меня есть две сетки с тремя рядами в каждой. Первая и последняя строка каждой сетки имеют фиксированную высоту, в то время как средние строки имеют автоматическую высоту и разделяют их высоту с помощью SharedSizeGroup.

Во-первых, содержимое правой сетки определяет высоту группы размеров. Когда содержимое правой сетки уменьшается, и содержимое левой сетки определяет высоту группы размеров, общий размер левой сетки корректируется неправильно.

См. Следующий пример приложения. Нажмите кнопку увеличения, чтобы увеличить размер текстового блока в правой сетке. Размер левой сетки изменяется соответственно. Теперь уменьшите размер. Когда текстовый блок становится меньше текстового блока в левой сетке, общий размер левой сетки не уменьшается. Это ошибка или я что-то упустил?

<StackPanel Orientation="Horizontal" Grid.IsSharedSizeScope="True">
    <StackPanel Orientation="Vertical" Width="100">
        <Grid Background="Green">
            <Grid.RowDefinitions>
                <RowDefinition Height="15" />
                <RowDefinition SharedSizeGroup="Group1" />
                <RowDefinition Height="15" />
            </Grid.RowDefinitions>
            <TextBlock Background="Blue" Grid.Row="0" />
            <TextBlock Background="Red" Grid.Row="1" Name="textBlock1" Height="100" />
            <TextBlock Background="Blue" Grid.Row="2" />
        </Grid>
        <TextBlock Text="{Binding Path=ActualHeight, ElementName=grid1}" />
    </StackPanel>
    <StackPanel Orientation="Vertical" Width="100">
        <Grid Background="Green">
            <Grid.RowDefinitions>
                <RowDefinition Height="15" />
                <RowDefinition SharedSizeGroup="Group1" />
                <RowDefinition Height="15" />
            </Grid.RowDefinitions>
            <TextBlock Background="Blue" Grid.Row="0" />
            <TextBlock Background="Red" Grid.Row="1" Name="textBlock2" Height="150" />
            <TextBlock Background="Blue" Grid.Row="2" />
        </Grid>
        <TextBlock Text="{Binding Path=ActualHeight, ElementName=grid2}" />
    </StackPanel>
    <Button Height="24" Click="Button_Click_1" VerticalAlignment="Top">Increase</Button>
    <Button Height="24" Click="Button_Click_2" VerticalAlignment="Top">Decrease</Button>
</StackPanel>

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    textBlock2.Height += 30;
}

private void Button_Click_2(object sender, RoutedEventArgs e)
{
    textBlock2.Height -= 30;
}

1 Ответ

2 голосов
/ 05 октября 2010

Строки имеют одинаковую высоту - размер второго TextBlock изменяется, а размер первого TextBlock остается равным 100.

Чтобы продемонстрировать это, внесите следующие изменения:

  • Добавьте ShowGridLines="True" к каждой из ваших сеток
  • Измените ваши именованные TextBlocks, чтобы показать их ActualHeight как их текст:

        <TextBlock Background="Red" Grid.Row="1" Name="textBlock2" Height="150"
                   Text="{Binding RelativeSource={RelativeSource Self}, Path=ActualHeight}"/>
    

Вы увидите, что строка SharedSizeGroup будет максимальным ActualHeight двух текстовых блоков.

Обновление: короткий проект, чтобы показать, что происходит
Я создал быстрый и грязный проект, чтобы показать, что происходит. Оказывается, что когда правая сетка становится меньше исходного размера, левая сетка выполняет Arrange, но не Measure. Я не уверен, что понимаю это полностью - я опубликовал дополнительный вопрос с тем же решением.

Вот решение, которое я создал на основе вашего оригинала. Он просто оборачивает базовую сетку в пользовательский класс, который выдает информацию (через событие) при вызове Measure и Arrange. В главном окне я просто помещаю эту информацию в список.

Классы 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>

Конструктор главного окна (только код с выделенным кодом)

public Window1 () { InitializeComponent (); * +1039 *

    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));
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...