问题描述
有一种分配特殊值的方法路径?
is there a way to assign a special value when a binding cannot be evaluated because of a null value in the property path ?
例如,如果我在一个类客户中有一个属性名称像这样:
For instance if I have a property Name in a class Customer and a binding like this :
{Binding CurrentCustomer.Name}
当 CurrentCustomer 为null时,我希望绑定生成字符串---。
When CurrentCustomer is null I'd like the binding to produce the string "---".
TargetNullValue和FallbackValue似乎没有办法。
"TargetNullValue" and "FallbackValue" don't seem to do the trick.
提前感谢您的帮助。
编辑:
其实我要做的是用新的源代码代替真实的,当它不可用。
实际情况如下:
In fact what I'm trying to do is substituting a new source value in place of the true when it is not available.The real scenario is the following :
一个bool值用于确定控件的可见性,但是当这个值不可达时,我想将其替换为false。
a bool value is used to determine the visibility of a control, but when this value is not attainable I'd like to replace it with "false".
这是一个完美模仿我的实际用例的图示:
Here is an illustration that perfectly mimics my real use-case :
MainPage.xaml.cs:
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: p>
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 不是解决方案,因为它只会更改生成的值而不是源值。
FallbackValue is not the solution because it would only change the generated value and not the source value.
Abe Heidebrecht 为 WPF 使用 PriorityBinding ,但在 Silverlight 中不存在。
Abe Heidebrecht has provided the perfect solution for WPF with PriorityBinding but it does not exist in Silverlight.
FINAL EDIT :
Abe Heidebrecht 的第二个解决方案,即包装在另一个元素中,工作正常。
FINAL EDIT :The second solution of Abe Heidebrecht, ie wrapping in another element, is working perfectly.
推荐答案
您可以使用 PriorityBinding :
<TextBlock>
<TextBlock.Text>
<PriorityBinding>
<Binding Path="CurrentCustomer.Name" />
<Binding Source="---" />
</PriorityBinding>
</TextBlock.Text>
</TextBlock>
好的,对于Silverlight,将元素包装在一个包装器(如Border) 。然后,您将有一个 IValueConverter
将null转换为 Visibility.Collapsed
,其他任何内容可以可见性.Visible
:
Okay, for Silverlight it is a probably easier to wrap the element in a wrapper (like a Border). You then have an IValueConverter
to convert null to Visibility.Collapsed
, and anything else to 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>
这篇关于由于空值而无法评估绑定时使用默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!