2010-11-23 44 views
16

的无法评估使用的默认值是没有指定一个特殊值绑定不能因为属性路径一个空值的评估的方法吗?绑定时,因为空值

举例来说,如果我有一个属性名称一类客户和这样的绑定:

{Binding CurrentCustomer.Name} 

CurrentCustomer为空,我想结合产生字符串“ ---”。

“TargetNullValue”和“FallbackValue”似乎没有办法。

在此先感谢您的帮助。

编辑

其实我想要做代替真正的被代以新源值当它是不可用的。 真实情况如下:

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不是解决方案因为e它只会改变生成的值而不是源值。

安倍Heidebrecht提供了完美的解决方案为WPFPriorityBinding,但它不能在的Silverlight存在。

FINAL EDIT安倍Heidebrecht的第二个解决方案,即在包裹另一个元件,被正常使用。

回答

20

你可以使用一个PriorityBinding

<TextBlock> 
    <TextBlock.Text> 
     <PriorityBinding> 
      <Binding Path="CurrentCustomer.Name" /> 
      <Binding Source="---" /> 
     </PriorityBinding> 
    </TextBlock.Text> 
</TextBlock> 

好,为Silverlight是一个可能更容易包裹元素的包装(如边界)。然后,您有一个IValueConverter为null转换为Visibility.Collapsed,和其他任何对Visibility.Visible

public class NullToVisibilityConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     return value != null ? Visibility.Visible : Visibility.Collapsed; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

并使用它像这样:

<Border Visibility="{Binding CurrentCustomer, Converter={StaticResource NullToVisibilityConverter}}"> 
    <TextBlock Text="You have a car" Visibility="{Binding CurrentCustomer.HasACar,Converter={StaticResource boolToVisibilityConverter}}" /> 
</Border> 
+0

我不知道你可以这样做....我一直使用DataTrigger检查值是否等于`{x:Null}`。谢谢! – Rachel 2010-11-23 19:38:01

+0

谢谢,这将有效地解决方案...如果我没有使用Silverlight 4,似乎不支持PriorityBinding。 – Pragmateek 2010-11-23 20:01:04

13

嘿TargetNullValue和FallbackValue工作。可能是你使用的.NET版本是错误的。

它需要的.NET Framework 3.5 SP1 .TargetNullValue和FallbackValue是一个新增加的绑定类,如果你使用.NET框架

0

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>