Wpf 不同窗口大小上的文本行对齐
我正在开发一个必须支持多种分辨率的登录窗口。 在那个窗口里有一长串不同颜色的文字Wpf 不同窗口大小上的文本行对齐,wpf,xaml,Wpf,Xaml,我正在开发一个必须支持多种分辨率的登录窗口。 在那个窗口里有一长串不同颜色的文字 <TextBlock Style="{StaticResource TextStyle}" TextWrapping="Wrap" HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center"> <Run Text="{
<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
我不知道如何支持这两种行为。因为我可以看到,您所需要的只是在第二条消息之前粘贴一个换行符,以获得高分辨率。因此,我将在具有值转换器的视图层中执行此操作:
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>
public类NewLineInsCnv:IValueConverter
{
public bool InsertNewLine{get;set;}=IsHighResolution();
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
if(InsertNewLine&&值为字符串st)
{
return Environment.NewLine+st;
}
返回值;
}
公共对象转换回(对象值、类型targetTypes、对象参数、CultureInfo区域性)
{
抛出新的NotImplementedException(“单向转换”);
}
私有静态布尔IsHighResolution()
{
返回true;//检查
}
}
如果需要,您可以将
InsertNewLine
设置为一个依赖属性。如果我是您,我会为窗口分辨率(它是静态的,因此您可以在绑定中引用它(使用x:static))编写一个触发器(在您的textblock中),该触发器将更改TextWrapping值。这听起来可能可行,你有没有可能找到参考链接?