可以将转换器附加到所有此类属性,这些属性具有FallbackValue
(在设计时使用)和Converter
(以提供运行时转换器)属性:
public class RuntimeConverter : MarkupExtension, IValueConverter
{
public object FallbackValue { get; set; }
public IValueConverter Converter { get; set; }
public RuntimeConverter() { }
public override object ProvideValue(IServiceProvider serviceProvider) => this;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
return FallbackValue;
if (Converter == null)
return value;
return Converter.Convert(value, targetType, parameter, culture);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
return FallbackValue;
if (Converter == null)
return value;
return Converter.ConvertBack(value, targetType, parameter, culture);
}
}
然后在设计时可以更改静态属性返回的值:
<!-- split in multiple lines for readability -->
Visibility="{Binding IsPowerUser, Source={x:Static local:User.Logged},
Converter={local:RuntimeConverter Converter={local:FalseToCollapsedConverter},
FallbackValue=Collapsed}}">