当我在WPF中调整窗口大小时,按钮会被剪裁

时间:2015-10-13 05:14:32

标签: c# wpf

我有一个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调整大小?

2 个答案:

答案 0 :(得分:0)

<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>

上面的代码没有剪辑按钮。 切记不要设置宽度和高度

PS:当Window宽度小于50并且在类似的情况下,它会剪切:)显而易见:) :( / p>

答案 1 :(得分:0)

删除保证金&amp;按钮的宽度。问题应该是。如果宽度小于75 + 120 + 75 + 20,它将开始剪切按钮。我建议不要使用宽度和宽度。保证金而是使用额外的行&amp;您的网格中的列&amp;相应地设置它。也给min&amp;最大高度或最小值用户控件的最大宽度,以便对其进行整体控制。

请参阅以下示例

<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>