TextBlock MultiBinding 抛出异常
TextBlock MultiBinding throws exception
[= 18 =]
这是我的方法:
<CheckBox Content="Display Subscriber" x:Name="DisplaySubscriber" />
<TextBlock Text="{Binding Path=SubscriberString, Mode=OneWay}" >
<TextBlock.Visibility>
<MultiBinding>
<Binding ElementName="DisplaySubscriber" Path="IsChecked" Mode="OneWay" Converter="{StaticResource BooleanToVisibleConverter}"/>
<Binding Path="SubscriberString" Mode="OneWay" Converter="{StaticResource StringIsNullEmptyConverter}"/>
</MultiBinding>
</TextBlock.Visibility>
</TextBlock>
但是我得到了这个错误:
Provide value on 'System.Windows.Data.MultiBinding' threw an
exception.
转换器:
public class TextBlockVisibilityConverter : IMultiValueConverter
{
#region IMultiValueConverter Members
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if((value[0] != null && (bool)value[0]) || (value[1]!=null && !String.IsNullOrEmpty(value[1].ToString())))
{
return Visibility.Visible;
}
return Visibility.Collapsed;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
#endregion
}
xaml:
在资源中定义转换器:
<local:TextBlockVisibilityConverter x:Key="TextBlockVisibilityConverter"/>
在多重绑定中使用它
<MultiBinding Converter="{StaticResource TextBlockVisibilityConverter}">
<Binding ElementName="DisplaySubscriber" Path="IsChecked"/>
<Binding Path="SubscriberString" />
</MultiBinding>
这是我的方法:
<CheckBox Content="Display Subscriber" x:Name="DisplaySubscriber" />
<TextBlock Text="{Binding Path=SubscriberString, Mode=OneWay}" >
<TextBlock.Visibility>
<MultiBinding>
<Binding ElementName="DisplaySubscriber" Path="IsChecked" Mode="OneWay" Converter="{StaticResource BooleanToVisibleConverter}"/>
<Binding Path="SubscriberString" Mode="OneWay" Converter="{StaticResource StringIsNullEmptyConverter}"/>
</MultiBinding>
</TextBlock.Visibility>
</TextBlock>
但是我得到了这个错误:
Provide value on 'System.Windows.Data.MultiBinding' threw an exception.
转换器:
public class TextBlockVisibilityConverter : IMultiValueConverter
{
#region IMultiValueConverter Members
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if((value[0] != null && (bool)value[0]) || (value[1]!=null && !String.IsNullOrEmpty(value[1].ToString())))
{
return Visibility.Visible;
}
return Visibility.Collapsed;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
#endregion
}
xaml: 在资源中定义转换器:
<local:TextBlockVisibilityConverter x:Key="TextBlockVisibilityConverter"/>
在多重绑定中使用它
<MultiBinding Converter="{StaticResource TextBlockVisibilityConverter}">
<Binding ElementName="DisplaySubscriber" Path="IsChecked"/>
<Binding Path="SubscriberString" />
</MultiBinding>