Wpf 由于空值而无法计算绑定时,请使用默认值
当由于属性路径中的空值而无法计算绑定时,是否有方法指定特殊值 例如,如果我在类Customer中有一个属性Name,并且有如下绑定:Wpf 由于空值而无法计算绑定时,请使用默认值,wpf,silverlight,data-binding,null,Wpf,Silverlight,Data Binding,Null,当由于属性路径中的空值而无法计算绑定时,是否有方法指定特殊值 例如,如果我在类Customer中有一个属性Name,并且有如下绑定: {Binding CurrentCustomer.Name} 当CurrentCustomer为空时,我希望绑定生成字符串“--” “TargetNullValue”和“FallbackValue”似乎不起作用 提前感谢你的帮助 编辑: 事实上,我试图做的是在true不可用时替换一个新的源值。 实际情况如下: bool值用于确定控件的可见性,但当无法获得此值时,
{Binding CurrentCustomer.Name}
当CurrentCustomer为空时,我希望绑定生成字符串“--”
“TargetNullValue”和“FallbackValue”似乎不起作用
提前感谢你的帮助
编辑:
事实上,我试图做的是在true不可用时替换一个新的源值。
实际情况如下:
bool值用于确定控件的可见性,但当无法获得此值时,我希望将其替换为“false”
下面是一个完全模仿我的真实用例的示例:
MainPage.xaml.cs:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace TestSilverlightBindingDefaultValue
{
public class BoolToVisibilityConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (bool)value ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
public class Customer
{
public bool HasACar
{
get;
set;
}
}
public partial class MainPage : UserControl
{
public static readonly DependencyProperty CurrentCustomerProperty =
DependencyProperty.Register("CurrentCustomer", typeof(Customer), typeof(MainPage), null);
public Customer CurrentCustomer
{
get { return this.GetValue(CurrentCustomerProperty) as Customer; }
set { this.SetValue(CurrentCustomerProperty, value); }
}
public MainPage()
{
InitializeComponent();
this.CurrentCustomer = null;
this.DataContext = this;
}
}
}
MainPage.xaml:
<UserControl x:Class="TestSilverlightBindingDefaultValue.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:TestSilverlightBindingDefaultValue"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<UserControl.Resources>
<local:BoolToVisibilityConverter x:Key="boolToVisibilityConverter" />
</UserControl.Resources>
<StackPanel x:Name="LayoutRoot" Background="White">
<TextBlock Text="You have a car" Visibility="{Binding CurrentCustomer.HasACar,Converter={StaticResource boolToVisibilityConverter}}" />
</StackPanel>
FallbackValue不是解决方案,因为它只会更改生成的值,而不会更改源值
Abe Heidebrecht为WPF提供了完美的解决方案,具有优先绑定,但在Silverlight中不存在
最终编辑:
Abe Heidebrecht的第二种解决方案(即用另一种元素包装)运行良好。您可以使用:
并像这样使用它:
<Border Visibility="{Binding CurrentCustomer, Converter={StaticResource NullToVisibilityConverter}}">
<TextBlock Text="You have a car" Visibility="{Binding CurrentCustomer.HasACar,Converter={StaticResource boolToVisibilityConverter}}" />
</Border>
嘿,TargetNullValue和FallbackValue有效。可能是您使用的.NET版本错误。
它需要.NET Framework 3.5 SP1。TargetNullValue和FallbackValue是绑定类的新添加项如果使用.NET Framework 3.5或更高版本,则可以使用TargetNullValue 在本例中,如果已创建名为BackgroundProperty的依赖项属性,则可以在绑定声明中使用targetNullvalue。 在本例中,我从ResourcesDictionary传递颜色
<Style x:Key="LabelAxisNameStyle" TargetType="{x:Type Label}">
<Setter Property="Background">
<Setter.Value>
<Binding Path="BackgroundProperty" TargetNullValue="{StaticResource LabelTitleBrush}"/>
</Setter.Value>
</Setter>
</Style>
我不知道你可以这么做。。。。我一直使用DataTrigger检查值是否等于{x:Null}
。谢谢谢谢,这将是有效的解决方案…如果我没有使用似乎不支持PriorityBinding的Silverlight 4。我已经更新了答案,以反映如何在Silverlight中执行此操作。再次感谢您的大力帮助,但是…Silverlight 4似乎也不支持DataTrigger!对绑定和样式的支持还没有完全实现,这让人非常恼火。如果没有人有另一个干净的解决方案,我会把你的答案标记为接受。哈哈,我是个白痴。我对它进行了更新,以便它能够正常工作。每次你需要这种功能的时候都要这么做,这真的很烦人。谢谢,你说得对,但我的真实场景更复杂;我会更新我最初的问题,让它更清楚。
<Border Visibility="{Binding CurrentCustomer, Converter={StaticResource NullToVisibilityConverter}}">
<TextBlock Text="You have a car" Visibility="{Binding CurrentCustomer.HasACar,Converter={StaticResource boolToVisibilityConverter}}" />
</Border>
<Style x:Key="LabelAxisNameStyle" TargetType="{x:Type Label}">
<Setter Property="Background">
<Setter.Value>
<Binding Path="BackgroundProperty" TargetNullValue="{StaticResource LabelTitleBrush}"/>
</Setter.Value>
</Setter>
</Style>