1,实现IValueConverter转换器接口
public class BooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool booleanValue = (bool)value;
bool parameterValue = System.Convert.ToBoolean(parameter);
//Tips传递过来的参数为false Start传递过来的参数true
if (!parameterValue)
return !booleanValue && !parameterValue ? Visibility.Visible : Visibility.Collapsed;
else if (parameterValue)
return booleanValue && parameterValue ? Visibility.Visible : Visibility.Collapsed;
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new Exception();
}
}
2,页面引入转换器资源
xmlns:BoolConvert="clr-namespace:Converts"
<Window.Resources>
<BoolConvert:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</Window.Resources>
<Image Width="250" Height="250" x:Name="GoInto" Source="{Binding StartImage}"
Visibility="{Binding IsReady, Converter={StaticResource BooleanToVisibilityConverter},ConverterParameter=true}"></Image>
备注:最终会将IsReady和ConverterParameter参数传递到BooleanToVisibilityConverter中进行转换;