wpf 如果结果返回 null,则给 TextBlock 默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16612622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Give TextBlock Default Value if result returns null
提问by KeyboardFriendly
Hello I am trying to give a default value to a textblock if the results returned are null
您好,如果返回的结果为空,我正在尝试为文本块提供默认值
Here is what I am trying!
这就是我正在尝试的!
All that returns is the String Format I set!
返回的只是我设置的字符串格式!
<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>
回答by LPL
You could use TargetNullValue Property. This will return TargetNullValuewithout StringFormatif the binding returns Null.
您可以使用TargetNullValue 属性。如果绑定返回 Null,这将返回TargetNullValue而没有StringFormat。
<TextBlock Text="{Binding Name, StringFormat='Item Name: {0}', TargetNullValue='No Name Found'}" />
回答by Walt Ritscher
You can use the TargetNullValueproperty directly in a binding.
您可以TargetNullValue直接在绑定中使用该属性。
<TextBox Text='{Binding Path=LastName, TargetNullValue="No name found."}' />
回答by TrialAndError
In my applications, I find it more reliable to bind my triggers to the actual object my control is bound to. So, if I am looking at Name in the VM for binding of the actual text, I would bind my data trigger to that as well.
在我的应用程序中,我发现将触发器绑定到我的控件绑定到的实际对象更可靠。因此,如果我在 VM 中查看 Name 以绑定实际文本,我也会将我的数据触发器绑定到该名称。
<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 Name}" Value="{x:Null}">
<Setter Property="FontStyle" Value="Italic"/>
<Setter Property="Text" Value="No Name Found" />
</DataTrigger>
<DataTrigger Binding="{Binding Name}" Value="">
<Setter Property="FontStyle" Value="Italic"/>
<Setter Property="Text" Value="No Name Found" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
回答by Nathan Wheeler
I would bind the TextBlock to a property of an object that didn't return null; make your property return a default value. It appears that you always want the FontStyle to be Italic, so I would just build that in outside the Triggers.
我会将 TextBlock 绑定到不返回 null 的对象的属性;使您的属性返回默认值。看来您总是希望 FontStyle 为斜体,所以我只会在触发器之外构建它。

