一: 前台XAML:
Visibility="{Binding IsVisibleControl,Converter={StaticResource BoolToVisiblityConverter}}">
(注意:IsVisibleControl为自定义 bool属性, 需要在ViewModel中定义一下, IsVisibleControl赋值true或者false也在ViewModel中完成)
二: 后台帮助方法类:
public class BoolToVisiblityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool)
{
var param = parameter as string;
//是否反转
var isInvert = false;
if (!string.IsNullOrEmpty(param))
{
bool.TryParse(param,out isInvert);
}
if ((bool)value)
{
return isInvert? Visibility.Collapsed : Visibility.Visible;
}
else
{
return isInvert? Visibility.Visible : Visibility.Collapsed;
}
}
return DependencyProperty.UnsetValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}