Wpf 仅当某些条件为真时,通过绑定将TextBlock设为粗体

Wpf 仅当某些条件为真时,通过绑定将TextBlock设为粗体,wpf,binding,Wpf,Binding,如何将TextBlock定义为FontStyle为粗体,通过绑定到bool <TextBlock Text="{Binding Name}" FontStyle="???"> 有没有办法实现这一点,或者我的模型属性是否应该为我进行翻译,而不是直接呈现bool呈现FontStyle。然后绑定到neweposodesavable,让转换器返回正确的值。我将创建一个属性,在其getter中返回字体样式。如果上述属性为false,则可以使其返回null。然后将字体样式xaml

如何将
TextBlock
定义为
FontStyle
为粗体,通过
绑定到
bool

<TextBlock 
   Text="{Binding Name}"
   FontStyle="???">

有没有办法实现这一点,或者我的模型属性是否应该为我进行翻译,而不是直接呈现
bool
呈现
FontStyle
。然后绑定到neweposodesavable,让转换器返回正确的值。

我将创建一个属性,在其getter中返回字体样式。如果上述属性为false,则可以使其返回null。然后将字体样式xaml绑定到该属性

您可以通过
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>
XAML:



确保在XAML中将转换器声明为资源。在这种情况下使用触发器

<TextBlock.Style>
    <Style TargetType="TextBlock">
        <Style.Triggers>
            <DataTrigger Binding="{Binding NewEpisodesAvailable}" Value="True">
                <Setter Property="FontWeight" Value="Bold"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</TextBlock.Style>

关于项目的条款:

请帮助我理解为什么此提示被评为无效。这是一个使用小型转换器的完美示例。你期待完整的工作源代码或提示,以便你可以继续吗?Nr1,thx的细节,我会给他们两个尝试,这两个概念都是新的我。太多了!精彩的回答突出了两个非常有用的方法!
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;
    }
}
<TextBlock FontWeight="{Binding IsEnable,
                        Converter={StaticResource BoolToFontWeightConverter}}"/>
<TextBlock.Style>
    <Style TargetType="TextBlock">
        <Style.Triggers>
            <DataTrigger Binding="{Binding NewEpisodesAvailable}" Value="True">
                <Setter Property="FontWeight" Value="Bold"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</TextBlock.Style>