c# - 如果结果返回 null,则为 TextBlock 提供默认值

标签 c# wpf xaml textblock

你好,如果返回的结果为空,我正在尝试为文本 block 提供默认值

这就是我正在尝试的!

返回的都是我设置的字符串格式!

 <TextBlock x:Name="NameTxtBlock" Grid.Column="0" Margin="0,0,40,0" FontFamily="Segoe UI" FontSize="14" Text="{Binding Name, StringFormat='Item Name: {0}'}"  Padding="2">
    <TextBlock.Style>
        <Style TargetType="TextBlock" >                                            
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=NameTxtBlock, Path=Text}" Value="{x:Null}">
                    <Setter Property="FontStyle" Value="Italic"/>
                    <Setter Property="Text" Value="No Name Found" />
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=NameTxtBlock, Path=Text}" Value="{x:Static System:String.Empty}">
                    <Setter Property="FontStyle" Value="Italic"/>
                    <Setter Property="Text" Value="No Name Found" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

最佳答案

你可以使用 TargetNullValue Property .如果绑定(bind)返回 Null,这将返回不带 StringFormatTargetNullValue

<TextBlock Text="{Binding Name, StringFormat='Item Name: {0}', TargetNullValue='No Name Found'}" />

关于c# - 如果结果返回 null,则为 TextBlock 提供默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16612622/

相关文章:

wpf - WPF TextBlock 元素和 Label 控件有什么区别?

wpf - PasswordBox 不假设样式

xaml - namespace 中不存在名称 "LocalizedStrings"

c# - WPF XAML - 文本框的设计时间和可见性

c# - OleDbDataReader = null

c# - ASP.NET Core 2.1 中的 UseStaticFiles、UseSpaStaticFiles 和 UseSpa 有什么区别?

c# - 像这样将图像添加到 WPF 中的 ListView ...?

c# - 为什么 *p++ = *p - a 会给出奇怪的结果?

c# - 如何实现只能由其封闭类创建的密封、公共(public)嵌套类?

c# - 将控件/UIElement 可见性正确且正确地绑定(bind)到 Property MVVM C#​​ WPF