简体   繁体   English

如何避免按钮上的文本在 UWP xaml 中被截断

[英]How to avoid text on a button from being cut off in UWP xaml

I have a button with MinWidth set as MinWidth="118" and that works fine.我有一个 MinWidth 设置为 MinWidth="118" 的按钮,效果很好。 But when I change the Text size in windows using Settings -> Accessibility -> Text Size -> change it to 200%, then text takes more space and that hard coded width is not sufficient so text gets cut off.但是,当我使用设置 -> 辅助功能 -> 文本大小 -> 将其更改为 200% 更改 windows 中的文本大小时,文本会占用更多空间,并且硬编码的宽度不够,因此文本会被截断。 So how can I set the width so that text never cuts off?那么如何设置宽度以使文本永远不会被切断? Also, I do not want to calculate the new width for 200% text size because, the text of the button can change without making any code changes since that text is service delivered.此外,我不想计算 200% 文本大小的新宽度,因为按钮的文本可以在不进行任何代码更改的情况下更改,因为该文本是服务交付的。 So is there any way to set the width so that text on the button never cuts off?那么有没有办法设置宽度,使按钮上的文本永远不会被切断?

<Button
    MinWidth="118"
    MinHeight="30"
    HorizontalAlignment="Left"
    HorizontalContentAlignment="Center"
    VerticalContentAlignment="Center"
    AutomationProperties.Name="{x:Bind ViewModel.PrimaryActionAutomationName, Mode=OneWay}"
    BorderThickness="1"
    Click="{x:Bind ViewModel.InvokePrimaryAction}"
    Content="{x:Bind ViewModel.PrimaryAction, Mode=OneWay}"
    CornerRadius="3"
    Style="{StaticResource AccentButtonStyle}" />

Thanks谢谢

So is there any way to set the width so that text on the button never cuts off?那么有没有办法设置宽度,使按钮上的文本永远不会被切断?

No, there is no such built-in feature for the Button control.不, Button控件没有这样的内置功能。 You still need to manage the Button width on your own.您仍然需要自己管理Button宽度。 Handle the DisplayInformation.DpiChanged Event and get the current ResolutionScale value.处理DisplayInformation.DpiChanged 事件并获取当前的ResolutionScale值。 Then you could change the width based on the value.然后您可以根据该值更改宽度。

 public MainPage()
    {
        this.InitializeComponent();

        DisplayInformation displayInformation = DisplayInformation.GetForCurrentView();
        displayInformation.DpiChanged += DisplayInformation_DpiChanged;
    }

    private void DisplayInformation_DpiChanged(DisplayInformation sender, object args)
    {
        DisplayInformation displayInformation = DisplayInformation.GetForCurrentView();
        ResolutionScale scale = displayInformation.ResolutionScale;
        switch (scale)
        {
            case ResolutionScale.Scale100Percent:
                //your logic
                break;
            case ResolutionScale.Scale200Percent:
                // your logic
                break;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM