如何使用BooleanToVisibilityConverter与可见作为默认值
本文关键字:默认值 何使用 BooleanToVisibilityConverter | 更新日期: 2023-09-27 18:02:44
这段代码工作得很好,但是按钮的可见性在设计中崩溃了。
如何将其设置为可见?
<!--Resources-->
<BooleanToVisibilityConverter x:Key="BoolToVis" />
<Button Visibility="{Binding Converter={StaticResource BoolToVis}, Source={x:Static local:ConfigUser.Prc}}" Grid.Row="1"/>
如果我得到了你想要的。你需要的是按钮出现在设计模式,也出现当你的布尔值在运行时设置为真。
除了布尔值:
之外,您还可以创建测试是否处于设计模式的转换器。using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
public class DesignVisibilityConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
if (value is bool) {
return ((bool) value) || DesignerProperties.GetIsInDesignMode(Application.Current.MainWindow)
? Visibility.Visible
: Visibility.Collapsed;
}
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
throw new NotImplementedException();
}
}
默认为可见。这是我的逆BoolToVis
[ValueConversion(typeof(bool), typeof(Visibility))]
public class InverseBooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (value != null && (bool)value) ?
Visibility.Collapsed : Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return (value != null) && ((Visibility)value == Visibility.Collapsed);
}
}