我正在使用BooleanToVisibilityConvertor来操纵ListBox DataTemplate中TextBlock的可见性。
这是我的XAML代码:
<phone:PhoneApplicationPage.Resources>
<Converters:BooleanToVisibilityConvertor x:Key="booleanToVisibilityConvertor"/>
</phone:PhoneApplicationPage.Resources>
<TextBlock Grid.Row="2" HorizontalAlignment="Right" Padding="0,0,7,0" Visibility="{Binding AverageConsumption, Converter={StaticResource booleanToVisibilityConvertor}}">
<Run Text="{Binding AverageConsumption}"/>
<Run Text="l./100 km."/>
</TextBlock>
背后的代码:
public class BooleanToVisibilityConvertor : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
{
if (value != null)
{
if (!string.IsNullOrEmpty(value.ToString()))
{
return Visibility.Visible;
}
}
return Visibility.Collapsed;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
这是正确的,但是我需要一个(反向)转换器用于放置在同一位置的另一个TextBlock(Grid.Row =“2”HorizontalAlignment =“Right”) 使用静态Text =“Partial Refueling”因此当{Binding AverageConsumption}不为null或为空时,第一个TextBlock将是可变的,第二个将被折叠,反之亦然。像这样:
class BooleanToVisibilityConvertorReversed : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
{
if (value != null)
{
if (!string.IsNullOrEmpty(value.ToString()))
{
return Visibility.Collapsed;
}
}
return Visibility.Visible;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
我试图在XAML中声明另一个转换器:
<phone:PhoneApplicationPage.Resources>
<Converters:BooleanToVisibilityConvertor x:Key="booleanToVisibilityConvertor"/>
<Converters:BooleanToVisibilityConvertorReversed x:Key="booleanToVisibilityConvertorReversed"/>
</phone:PhoneApplicationPage.Resources>
但在InitializeComponent()上有一个例外: '在System.Windows.ni.dll中发生'System.Windows.Markup.XamlParseException'类型的第一次机会异常' 我该如何解决这个问题?
答案 0 :(得分:0)
好像我发现了你的问题。您的BooleanToVisibilityConvertorReversed
课程不公开。公开,你的问题应该消失。