【问题标题】:Buttons get clipped when I resize the window in WPF当我在 WPF 中调整窗口大小时,按钮被剪裁
【发布时间】:2016-01-10 17:29:35
【问题描述】:

我有一个设置了 MinWidth 和 MinHeight 的 UserControl。

里面有一个网格和许多控件。

我在主网格中有另一个Grid。内部网格的代码如下。

    <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="30"/>
    </Grid.RowDefinitions>

   <DataGrid Grid.Row="0" helpers:RowHeaderNumber.DisplayRowNumber="True" AutoGenerateColumns="False"  
              RowHeaderWidth="40" CanUserSortColumns="False" CanUserResizeColumns="False"
              Margin="10,65,20,0" ItemsSource="{Binding ExpressionCollection, Mode=TwoWay}"
              SelectionUnit="CellOrRowHeader" SelectionChanged="ExpressionGrid_SelectionChanged" PreviewMouseRightButtonUp="ExpressionGrid_PreviewMouseRightButtonUp" BorderBrush="{x:Null}" VerticalAlignment="Top"  >
     <DataGrid.RowHeaderStyle>
            <Style TargetType="DataGridRowHeader">
                <Setter Property="ContextMenu" Value="{StaticResource ExpressionContextMenu}"/>
                <Setter Property="Padding" Value="4,0,0,20"/>
                <Setter Property="Background" Value="#FFF1F0F0"/>
                <Setter Property="BorderBrush" Value="Black"/>
                <Setter Property="BorderThickness" Value="1,0,1,1"/>
            </Style>
        </DataGrid.RowHeaderStyle>
    </DataGrid>
    <Button Grid.Row="1" Content="OK" HorizontalAlignment="Right"  VerticalAlignment="Bottom" Width="75" Click="Ok_Button_Click" Margin="0,0,112,10"/>
    <Button Grid.Row="1" Content="Cancel" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="75" Click="Cancel_Button_Click" Margin="0,0,20,10"/>
    </Grid>

当我调整窗口大小时,底部的按钮会被剪掉。调整窗口大小时,我应该进行哪些更改以使 DataGrid 调整大小?

【问题讨论】:

  • DataGrids HorizontalAlignmentVerticalAlignment 设置为Stretch
  • @faztp12 按钮仍然被剪掉。
  • 是否为DataGrid 设置了WidthHeight?如果是这样,请将其删除
  • 没有。我只为DataGrid 设置了RowHeaderWidth。这应该不是问题。
  • 你能用完整的 XAML 更新你的问题吗? :)

标签: c# wpf


【解决方案1】:
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition Height="30"/>
    </Grid.RowDefinitions>

    <DataGrid Grid.Row="0"/>

    <Button Grid.Row="1" HorizontalAlignment="Left" Content="Button 1"/>
    <Button Grid.Row="1" Content="Button 2" HorizontalAlignment="Left" Margin="55,0,0,0"/>

</Grid>

上面的代码没有剪裁按钮。 切记不要设置Width和Height

PS: 当Window 宽度小于 50 并且在类似的情况下会剪辑:) 很明显 :)

【讨论】:

  • 好的。它仍然是我系统中的剪辑。无论如何,感谢您的宝贵时间。
  • :(我会玩一段时间,让你知道我哪里出错了。
  • 边距添加到所有控件。这会是个问题吗?
  • 没有。该行作为其内容的 Grid,Margin 将它们放在行内,就像在普通网格中一样:)
【解决方案2】:

从按钮中删除边距和宽度。问题应该是。如果宽度小于 75+120 + 75 + 20,它将开始剪裁按钮。我建议不要使用宽度和边距,而是在网格中使用额外的行和列并相应地设置它。还要为您的用户控件提供最小和最大高度或最小和最大宽度以对其进行整体控制。

看下面的例子

<Window x:Class="Test1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="100" Width="200" MinHeight="100" MinWidth="100">
    <Grid>
    <Grid.ColumnDefinitions>
         <ColumnDefinition/>
          <ColumnDefinition/>
    </Grid.ColumnDefinitions>
        <Button Width="50" Margin="20"></Button>
        <Button Width="50" Margin="20" Grid.Column="1"></Button>
    </Grid>
</Window>


in This example buttons will always clip when you resize & decrease the size of your window.

但如果你这样使用它永远不会剪辑

<Window x:Class="Test1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="100" Width="200" MinHeight="100" MinWidth="100">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Button></Button>
    <Button Grid.Column="1"></Button>
</Grid>

【讨论】:

    猜你喜欢
    • 2016-10-27
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 2011-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多