不同窗口大小的文本行对齐

时间:2018-11-26 07:52:57

标签: wpf xaml

我正在使用一个必须支持多种分辨率的登录窗口。 在该窗口中,有一长行不同颜色的文本。

<TextBlock   
    Style="{StaticResource TextStyle}"
    TextWrapping="Wrap" HorizontalAlignment="Center" 
    VerticalAlignment="Center"
    TextAlignment="Center">
        <Run  
            Text="{Binding MsgA}"
        />
        <Run                        
            Foreground="#FFFFFF"                      
            FontFamily="{StaticResource CentralSansBook}"                  
            Text="{Binding MsgB}"
        />
</TextBlock>

使用此代码,我得到: 在低分辨率下,因为我的宽度几乎是原来的一半,所以我会放弃漂亮的格式,只希望它适合,所以看起来像这样

This is message A.This is
      message B

在高分辨率下,我希望它看起来像这样

    This is message A.
    This is message B

我不知道如何支持这两种行为。

1 个答案:

答案 0 :(得分:0)

正如我所看到的,您只需要在第二条消息之前粘贴换行符即可获得高分辨率。因此,我将在View层使用值转换器进行操作:

public class NewLineInsCnv : IValueConverter
{
    public bool InsertNewLine { get; set; } = IsHighResolution();

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (InsertNewLine && value is string st)
        {
            return Environment.NewLine + st;
        }
        return value;
    }

    public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException("One way only conversion");
    }

    private static bool IsHighResolution()
    {
        return true;//Check
    }
}

<TextBlock   
        Style="{StaticResource TextStyle}"
        TextWrapping="Wrap" HorizontalAlignment="Center" 
        VerticalAlignment="Center"
        TextAlignment="Center">
        <TextBlock.Resources>
            <local:NewLineInsCnv x:Key="insertNewLineCnv"/>
        </TextBlock.Resources>
        <Run  
            Text="{Binding MsgA}"
        />
        <Run                        
            Foreground="#FFFFFF"                      
            FontFamily="{StaticResource CentralSansBook}"                  
            Text="{Binding MsgB, Converter={StaticResource insertNewLineCnv}}"
        />
    </TextBlock>

如有必要,可以将InsertNewLine设置为依赖项属性。

相关问题