我有下面的Xaml代码,我试图根据布尔条件为文本块内容加下划线。当条件为真时 它按预期工作(下划线可见(但当条件为假时,下划线仍然存在(当条件为假时,下划线不应可见(
<TextBlock Text="Name" TextDecorations="{x:Bind Model.NameError, Converter={StaticResource TextUnderlineConverter}, Mode=OneWay}"
转换器代码
public class TextUnderlineConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if ((bool)value)
return TextDecorations.Underline;
else
return TextDecorations.None;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
@Venkat 感谢您的反馈。这是一个已知问题。相关团队一直在调查此问题。
目前有一种解决方法,您可以为TextBlock
下的Run
节点设置TextDecorations
。
<TextBlock>
<Run Text="Test Hello" TextDecorations="{x:Bind Flag, Converter={StaticResource ConverterText},Mode=OneWay}" />
</TextBlock>
UWP 中的错误,如下面的 Xaml:
<TextBlock>
<Run Text="Decorations can be toggled on and off"/>
</TextBlock>
<TextBlock Text="Decorations will not toggle off"/>
如果使用 C# 编写文本块,则会出现相同的问题
TextBlock textBlock = new TextBlock { FontSize = 18.0 };
textBlock.Inlines.Add(new Windows.UI.Xaml.Documents.Run { Text = "This text will not stick on text decoration." });
TextBlock textBlockBad = new TextBlock
{
FontSize = 18.0,
Text = "This text will not enable the TextDecorations to be turned off"
};