首页 > 解决方案 > WPF 网格列 def auto 总是从右边剪裁

问题描述

我有一个 WPF 应用程序,它有一个网格,其中 2 列设置为 * 和 auto。问题是当我减小窗口的大小时,第二列中的子项从右侧而不是左侧被剪裁。我希望它们从左侧剪辑,因为我已将水平对齐设置为右侧。有没有办法我们可以从左边剪掉第二列元素?

<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" >
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" MinWidth="60"/>
            <ColumnDefinition Width="auto"/>
        </Grid.ColumnDefinitions>
        <DockPanel  HorizontalAlignment="Right" Grid.Column="1">
            <Button Click="Button_Click" Content="click me" Width="150" DockPanel.Dock="Right" />
            <Label Content="abcdef" Width="200" DockPanel.Dock="Right" />
            <Label x:Name="mLog"/>
        </DockPanel>
        <Button Click="Button_Click" DockPanel.Dock="Right" Content="click me" Width="150"/>
        <Label Content="abcdef" Width="200" DockPanel.Dock="Right"/>
    </Grid>
</Window>

在此处输入图像描述

标签: c#wpf

解决方案


而不是这个:

<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" >
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" MinWidth="60"/>
            <ColumnDefinition Width="auto"/>
        </Grid.ColumnDefinitions>
        <DockPanel  HorizontalAlignment="Right" Grid.Column="1">
            <Button Click="Button_Click" Content="click me" Width="150" DockPanel.Dock="Right" />
            <Label Content="abcdef" Width="200" DockPanel.Dock="Right" />
            <Label x:Name="mLog"/>
        </DockPanel>
        <Button Click="Button_Click" DockPanel.Dock="Right" Content="click me" Width="150"/>
        <Label Content="abcdef" Width="200" DockPanel.Dock="Right"/>
    </Grid>
</Window>

尝试这个:

<Window x:Class="WpfApp2.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp2"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800" >
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" MinWidth="60"/>
                <ColumnDefinition Width="auto"/>
            </Grid.ColumnDefinitions>
            <DockPanel  HorizontalAlignment="Right" Grid.Column="1">
                <Button Click="Button_Click" Content="click me" Width="150" DockPanel.Dock="Right" />
                <Label Content="abcdef" Width="200" DockPanel.Dock="Right" />
                <Label x:Name="mLog"/>
            </DockPanel>
            <Button Click="Button_Click" DockPanel.Dock="Right" Content="click me"/>
            <Label Content="abcdef" Width="200" DockPanel.Dock="Right"/>
        </Grid>
    </Window>

我删除了最后一个按钮宽度属性。

输出: wpf网格


推荐阅读