你可以像这样制作自己的转换器,这将根据角色改变可见性
public class RoleToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var user= value as User;
if(user!= null) {
return user.IsInRole((string)parameter) ? Visibility.Visible : Visibility.Collapsed;
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
在您的 XAML 中,您可以使用转换器来设置控件的可见性,如下所示
<Control Visibility={Binding Path=CurrentUser, Converter={StaticResource RoleToVisibilityConverter}, ConverterParameter=Student }/>