处理 WPF 应用程序中由于显示属性而导致的不同字体大小?

发布于 2024-07-19 03:41:09 字数 280 浏览 5 评论 0原文

我的小组正在 WPF 中构建一个编辑器类型的应用程序。 我们注意到的一件事是,在我的 WinXP 机器上,运行“Windows 经典风格”主题时,按钮上的文本非常合适。 然而,在我朋友的机器上,他运行的是“windows xp style”主题,字体大小较大,因此按钮上的文本会被剪裁在底部。

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

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

谢谢!

My group is building an editor-type app in WPF. One thing we noticed is that on my WinXP machine, running with the "windows classic style" theme, the text on buttons is fits fine. However on my friend's machine, who's running with the "windows xp style" theme, the font size is bigger so text on buttons get clipped at the bottom.

Is there a way to handle this nicely, like automatically resizing controls to fit the text?

I hesitate to manually resize the button to fit his layout as anyone else can have totally different settings through the Display Properties and Accessibility Options.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

完美的未来在梦里 2024-07-26 03:41:09

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> 

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

A WPF button will automatically resize to fit the content that it has been given, however it will only do this when it is inside a container that does not enforce size and its size has not been set manually. To prove this mess around with the font size in the following code snippet:

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

I guess that your buttons haven't resized because you have constrained them. To fix this you need to decide how you want them to resize (which can be very complicated when elements would overlap if they just grew blindly) and if none of the supplied panel types perform the growth behaviour that you are looking for then you may need to write your own that does.

真心难拥有 2024-07-26 03:41:09

您是否使用 Width 和 Height 属性对元素大小进行了硬编码? 在 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>

Have you hardcoded element sizes using Width and Height properties? In WPF the recommended way to do this is to use the several layout containers.

The following is an example of a grid which lays two buttons at the bottom and a textbox at the top.

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文