我的小组正在WPF中构建一个编辑器类型的应用程序。我们注意到的一件事是,在我的WinXP机器上,使用“windows classic style”运行。主题,按钮上的文字很合适。但是在我朋友的机器上,谁正在使用“windows xp style”运行主题,字体大小更大,因此按钮上的文字被剪切在底部。

有没有办法很好地处理这个问题,比如自动调整控件以适应文本?

我毫不犹豫地手动调整按钮的大小以适应他的布局,因为其他任何人都可以通过“显示属性”和“辅助功能选项”进行完全不同的设置。

谢谢!

有帮助吗?

解决方案

WPF按钮自动调整大小以适应已经给出的内容,但只有当它位于不强制执行大小且未设置其大小的容器内时才会执行此操作手动。要在下面的代码片段中使用字体大小来证明这种混乱:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Button
        Grid.Column="1"
        Grid.Row="1"
        FontSize="24"
        Content="QWERTY"/>
</Grid> 

我猜你的按钮没有调整大小,因为你限制了它们。要解决这个问题,你需要决定你希望它们如何调整大小(如果元素只是盲目地增长,这可能会非常复杂)如果所提供的面板类型都没有执行你正在寻找的增长行为,那么你可能需要写你自己的。

其他提示

您是否使用“宽度”和“高度”属性对元素大小进行了硬编码?在WPF中,建议的方法是使用多个布局容器。

以下是一个网格示例,它在底部放置两个按钮,在顶部放置一个文本框。

<Grid>
    <Grid.RowDefinitions>
        <!-- TextBox row with unspecified height. -->
        <RowDefinition Height="*"/>

        <!-- Button row with automated height so it resizes to
             fit the content -->
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <!-- Textbox on first row. -->
    <TextBox Margin="3" Name="textBox1" Grid.Row="0" AcceptsReturn="True" />

    <!-- StackPanel which lays the two buttons at the bottom horizontally.
         RightToLeft is specified so that the first button appears on right.
         -->
    <StackPanel Grid.Row="1" HorizontalAlignment="Right"
                Orientation="Horizontal" FlowDirection="RightToLeft">

        <!-- The buttons. Only padding and margin are hardcoded so these
             can resize to the contents -->
        <Button Padding="3" Margin="3">OK</Button>
        <Button Padding="3" Margin="3">Cancel</Button>
    </StackPanel>
</Grid>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top