的无法评估使用的默认值是没有指定一个特殊值当绑定不能因为属性路径一个空值的评估的方法吗?绑定时,因为空值
举例来说,如果我有一个属性名称一类客户和这样的绑定:
{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提供了完美的解决方案为WPF与PriorityBinding,但它不能在的Silverlight存在。
FINAL EDIT: 安倍Heidebrecht的第二个解决方案,即在包裹另一个元件,被正常使用。
我不知道你可以这样做....我一直使用DataTrigger检查值是否等于`{x:Null}`。谢谢! – Rachel 2010-11-23 19:38:01
谢谢,这将有效地解决方案...如果我没有使用Silverlight 4,似乎不支持PriorityBinding。 – Pragmateek 2010-11-23 20:01:04