【问题标题】:How to make Border controls to have the same width inside a single Grid control?如何使边框控件在单个 Grid 控件中具有相同的宽度?
【发布时间】:2011-12-30 16:59:49
【问题描述】:

我有这样的事情:

<MyView>  
    <Style TargetType="Border" x:Key="Module">
      <Setter Property="BorderThickness" Value="1" />
      <Setter Property="BorderBrush" Value="Gray" />
      <Setter Property="Padding" Value="10" />
      <Setter Property="Margin" Value="10" />
    </Style>    
    <Grid>
      <Grid.ColumnDefinitions>        
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
      </Grid.RowDefinitions>
      <Border Name="Border1" Style="{StaticResource Module}">
        <!--some controls with non-fixed width-->
      </Border>
      <Border Grid.Row="1" Name="Border2" Style="{StaticResource Module}">
        <!--some controls with non-fixed width-->
      </Border>
      <Border Grid.Column="1" Grid.RowSpan="2" Style="{StaticResource Module}" Name="Border3">
        <!--some controls with non-fixed width-->
      </Border>
    </Grid>
</MyView>

Border1和Border2里面的控件可能有不同的宽度,所以它们的边框也会有不同的宽度,看起来不太好。如何强制 Border1 和 Border2 边框具有相同的宽度以便看起来更好?

由于Border1和Border2的子控件宽度可能不同,手动设置相同宽度的解决方案不计算在内。

而 Border3 的内容只是占用了所有可用空间,这很好。

【问题讨论】:

    标签: wpf grid width border


    【解决方案1】:

    &lt;Setter Property="HorizontalAlignment" Value="Stretch" /&gt; 添加到您的边框样式

    【讨论】:

    • 当我将 Border1 的宽度设置为 *.并且“您现在拥有的意味着第一列将仅占用所需的空间,而第二列将占用所有剩余空间。”正是我想要的,所以很遗憾你的解决方案不能解决我的问题。
    • 抱歉,刚刚注意到您的边框在您的行中,而不是在您的列中。设置您的边框HorizontalAlginment="Stretch",它应该可以工作
    猜你喜欢
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多