如何通过a到a定义a TextBlock
asFontStyle
粗体?Binding
bool
<TextBlock
Text="{Binding Name}"
FontStyle="???">
我真的很想将它绑定到
public bool NewEpisodesAvailable
{
get { return _newEpisodesAvailable; }
set
{
_newEpisodesAvailable = value;
OnPropertyChanged();
}
}
有没有一种方法来实现这一目标,还是应该我Model属性为我做的,而不是呈现的翻译,bool
本FontStyle
直接?
您可以这样实现DataTrigger
:
<TextBlock>
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding NewEpisodesAvailable}"
Value="True">
<Setter Property="FontWeight" Value="Bold"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
或者,您可以使用IValueConverter将布尔转换为FontWeight。
public class BoolToFontWeightConverter : DependencyObject, IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
return ((bool)value) ? FontWeights.Bold : FontWeights.Normal;
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
return Binding.DoNothing;
}
}
XAML:
<TextBlock FontWeight="{Binding IsEnable,
Converter={StaticResource BoolToFontWeightConverter}}"/>
确保在XAML中将转换器声明为资源。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句