class BoolToVisibilityConverter:IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
bool? b = (bool?)value;
//如果传入的值为true,则返回Visible
if (b is true)
{
return Visibility.Visible;
}
//否则返回Collapsed
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
<Page.Resources>
<local:BoolToVisibilityConverter x:Key="cvt"/>
</Page.Resources>
<StackPanel>
<CheckBox x:Name="cb"/>
<Rectangle Width="130" Height="100" Fill="SkyBlue" Visibility="{Binding ElementName=cb,Path=IsChecked}"/>
</StackPanel>