例如,如果我在一个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的第二个解决方案,即包装在另一个元素中,工作正常.
<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>总结
以上是内存溢出为你收集整理的wpf – 使用默认值,因为null值无法进行绑定全部内容,希望文章能够帮你解决wpf – 使用默认值,因为null值无法进行绑定所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)