1.编写转换类Bool2VisibilityConverter,继承自IValueConverter(using System.Windows.Data;)
public class Bool2VisibilityConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value != null && bool.TryParse(value.ToString(), out bool result) && result) return Visibility.Visible; return Visibility.Collapsed; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }View Code
2.前端页面在UserControl.Resources节点中进行转换器的引用定义
<UserControl.Resources> <converter:Bool2VisibilityConverter x:Key="Bool2VisibilityConverter"/> </UserControl.Resources>View Code
3.根据bool字段绑定判断是否显示
<Grid VerticalAlignment="Top" Margin="15,0" Visibility="{Binding PageModel.IsShowDetail, Converter={StaticResource Bool2VisibilityConverter}}"> </Grid>
标签:object,前端,中后,value,bool,Visibility,public From: https://www.cnblogs.com/jiangyuhu/p/18671048