我不知道该怎么做,而且我相信它不是为那样工作而设计的。
您可以更改您的 Converter 以实现 IMultiValueConverter 并使用 Window 属性 IsLoaded 作为第二个参数这样做。
创建一个 DependencyProperty,例如 WindowIsLoaded,然后在 Loaded 事件中将值更改为 真。
在Converter上,当WindowIsLoaded等于false时总是返回Visibility.Visible.
开启代码:
public bool WindowIsLoaded
{
get { return (bool)GetValue(WindowIsLoadedProperty); }
set { SetValue(WindowIsLoadedProperty, value); }
}
public static readonly DependencyProperty WindowIsLoadedProperty =
DependencyProperty.Register("WindowIsLoaded", typeof(bool), typeof(Window),
new PropertyMetadata(false));
private void Window_Loaded(object sender, RoutedEventArgs e)
{
WindowIsLoaded = true;
}
public class BooleanToVisibilityConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var isActivated = (bool)values[0];
var isLoaded = (bool)values[1];
if (!isLoaded)
return Visibility.Visible;
return isActivated ? Visibility.Visible : Visibility.Collapsed
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new Exception("Only oneway binding!");
}
}
XAML:
xmlns:local="clr-namespace:YourProjectNamespace.YourWindow"
<Canvas x:Name="groupControls">
<Canvas.Visibility>
<MultiBinding Converter="{StaticResource BooleanToVisibilityConverter}">
<Binding ElementName="MyControl" Path="IsActivated"/>
<Binding RelativeSource="{RelativeSource AncestorType={x:Type local:MainWindow},
Mode=FindAncestor}"
Path="WindowIsLoaded" />
</MultiBinding>
</Canvas.Visibility>
</Canvas>
现在 Designer 将从 WindowIsLoaded 接收 false 并且您的所有控件都将在 上可见设计师模式.