Сетка не растягивается внутри StackPanel - PullRequest
0 голосов
/ 30 декабря 2018

У меня есть два текстовых поля с постоянной высотой, я хочу вертикально сложить текстовые поля и сетку.Я пробовал использовать стековую панель, но затем сетка не растягивается, и она все время остается с одним и тем же размером (как можно меньшим).

<StackPanel Orientation="Vertical" MaxWidth="110">
        <TextBox  Background="White" Height="40" Text="some text1"/>
        <TextBox  Background="White" Height="40" Text="some text2"/>
        <Grid x:Name="internalGrid">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*" MaxWidth="110"/>
                <ColumnDefinition Width="1*" MaxWidth="110"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="1*" MaxHeight="300"/>
                <RowDefinition Height="1*" MaxHeight="300"/>
            </Grid.RowDefinitions>
            <Button Grid.Column="0" Grid.Row="0"/>
            <Button Grid.Column="0" Grid.Row="1"/>
            <Button Grid.Column="1" Grid.Row="0"/>
            <Button Grid.Column="1" Grid.Row="1"/>
        </Grid>
    </StackPanel>

Я также пытался использовать Grid вместо стековой панели, но потом, когдаВ приложении с полноэкранным режимом есть поле между текстовыми полями и внутренней сеткой

Ответы [ 2 ]

0 голосов
/ 30 декабря 2018

Вы можете достичь этого, используя IMultiValueConverter:

[ValueConversion(typeof(double), typeof(double))]
public class MyConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        var total = (double)values.FirstOrDefault();
        var subtract = values.Cast<double>().Sum();
        return total + total - subtract;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

. Вы можете представить свой конвертер в XAML в разделе ресурсов:

<Window.Resources>
    <local:MyConverter x:Key="Converter"></local:MyConverter>
</Window.Resources>

И ваш XAML будет измененto:

<StackPanel Orientation="Vertical" MaxWidth="110" Background="Red">
        <TextBox Name="Label1" Background="White" Height="40" Text="some text1"/>
        <TextBox Name="Label2" Background="White" Height="40" Text="some text2"/>
        <Grid x:Name="internalGrid" Background="Yellow" >
            <Grid.Height>
                <MultiBinding Converter="{StaticResource Converter}">
                    <Binding RelativeSource="{RelativeSource AncestorType=StackPanel}" Path="ActualHeight"/>
                    <Binding ElementName="Label1" Path="ActualHeight"/>
                    <Binding ElementName="Label2" Path="ActualHeight"/>
                </MultiBinding>
            </Grid.Height>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*" MaxWidth="110"/>
                <ColumnDefinition Width="1*" MaxWidth="110"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="1*" MaxHeight="300"/>
                <RowDefinition Height="1*" MaxHeight="300"/>
            </Grid.RowDefinitions>
        </Grid>
    </StackPanel>

Определенно, цвета фона просто для большей ясности и не нужны.

0 голосов
/ 30 декабря 2018

StackPanel не ведет себя так.Занимает минимально необходимое пространство.Вместо этого используйте Grid и определите RowDefinition s.По умолчанию пространство внутри сетки равномерно распределяется между строками (высота имеет значение «*»), поэтому необходимо установить для высоты значение «Авто», чтобы строки занимали минимальное пространство:

  <Grid MaxWidth="110">
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition Height="Auto" />
      <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <TextBox Grid.Row="0" Background="White"
              Height="40"
              Text="some text1" />
    <TextBox Grid.Row="1" Background="White"
              Height="40"
              Text="some text2" />

    <Grid Grid.Row="2" x:Name="internalGrid">
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*"
                          MaxWidth="110" />
        <ColumnDefinition Width="1*"
                          MaxWidth="110" />
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
        <RowDefinition Height="1*"
                       MaxHeight="300" />
        <RowDefinition Height="1*"
                       MaxHeight="300" />
      </Grid.RowDefinitions>

      <Button Grid.Column="0"
              Grid.Row="0" />
      <Button Grid.Column="0"
              Grid.Row="1" />
      <Button Grid.Column="1"
              Grid.Row="0" />
      <Button Grid.Column="1"
              Grid.Row="1" />
    </Grid>
  </Grid>

Или попробуйте DockPanel:

<DockPanel MaxWidth="110" LastChildFill="True" >
    <TextBox DockPanel.Dock="Top" Background="White"
              Height="40"
              Text="some text1" />
    <TextBox DockPanel.Dock="Top" Background="White"
              Height="40"
              Text="some text2" />
    <Grid DockPanel.Dock="Top" x:Name="internalGrid">
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*"
                          MaxWidth="110" />
        <ColumnDefinition Width="1*"
                          MaxWidth="110" />
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
        <RowDefinition Height="1*"
                       MaxHeight="300" />
        <RowDefinition Height="1*"
                       MaxHeight="300" />
      </Grid.RowDefinitions>
      <Button Grid.Column="0"
              Grid.Row="0" />
      <Button Grid.Column="0"
              Grid.Row="1" />
      <Button Grid.Column="1"
              Grid.Row="0" />
      <Button Grid.Column="1"
              Grid.Row="1" />
    </Grid>
  </DockPanel>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...