WPF根据绑定的文本框内容变化更改标签显示

3
我有一个绑定的文本框,需要在其包含任何内容时将其关联的显示标签加粗。如有可能,我不想使用JavaScript。
谢谢。
1个回答

4

为什么要涉及JavaScript?

无论如何,如果这确实是wpf,请使用自定义转换器将标签的FontWeight属性绑定到文本框的文本属性,将空值/空字符串转换为普通字体重量,将非空/非空字符串转换为粗体。


我一直在搜索并看到一个类似的问题,它是用js解决的 ;) - RobDog888
谢谢,听起来这个方法可行,但这个文本框已经绑定了一个自定义转换器。我可以使用多个吗?如果可以,怎么做?谢谢。 - RobDog888
这不是文本框上的转换器,而是标签上的转换器。类似于 <Label FontWeight="{Binding ElementName=TextBox,Path=Text,Converter={StaticResource ConvertNonEmptyTextToBoldConverter}}"/> 或者其他类似的东西。 - John Gardner
谢谢你,约翰。这就是我最终使用的内容...//标签控件: FontWeight="{Binding Path=TextNumber, Converter={StaticResource fontWeightConverter}}"还有转换器... - RobDog888
class FontWeightConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var sourceValue = (string)value; if (!string.IsNullOrEmpty(sourceValue)) { return System.Windows.FontWeights.Bold; } else return System.Windows.FontWeights.Normal; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException("ConvertBack不支持"); } } - RobDog888

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接