微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

wpf – 使用默认值,因为null值无法进行绑定

属性路径中由于空值而无法评估绑定时,是否有分配特殊值的方法

例如,如果我在一个Customer类中有一个属性Name,并且这样一个绑定:

{Binding CurrentCustomer.Name}

当CurrentCustomer为null时,我希望绑定生成字符串“—”.

“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,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为PriorityBinding提供了完美的WPF解决方案,但它不存在于Silverlight中.

最终编辑:
Abe Heidebrecht的第二个解决方案,即包装在另一个元素中,工作正常.

解决方法

你可以使用 PriorityBinding

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

好的,对于Silverlight来说,很容易将元素包装在包装器中(像Border).然后您有一个IValueConverter将null转换为Visibility.Collapsed,还有其他任何Visibility.Visible:

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

    public object ConvertBack(object value,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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐