【问题标题】:Preventing WPF to evaluate control during design time防止 WPF 在设计时评估控制
【发布时间】:2023-03-30 23:08:01
【问题描述】:

我在一个单独的项目中有自定义控件。在我的主要项目中,一些控件的可见性属性连接到此控件。例如:

<Canvas x:Name="groupControls" Visibility="{Binding IsActive, ElementName=MyControl, Converter={StaticResource BooleanToVisibilityConverter}}">

在设计期间,该属性返回 false 并且 groupControls 变得不可见,因此我看不到它。我想阻止设计师评估该属性,就好像它是在同一个项目中定义的一样。

使用 DesignerProperties.IsInDesignModeProperty 并不能解决问题,因为有些控件在值为 true 时可见,而有些在值为 false 时可见。

【问题讨论】:

    标签: wpf custom-controls visibility evaluation


    【解决方案1】:
    <Canvas x:Name="groupControls" Visibility="{Binding IsActive,
                                                        FallbackValue=True,                
                                                        ElementName=MyControl, 
                                                        Converter={StaticResource BooleanToVisibilityConverter}}">
    

    或者你需要的是设计模式

    Is there a way to check if WPF is currently executing in design mode or not?

    【讨论】:

      【解决方案2】:

      我不知道该怎么做,而且我相信它不是为那样工作而设计的。

      您可以更改您的 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 并且您的所有控件都将在 上可见设计师模式.

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-01
        • 2012-08-30
        • 1970-01-01
        相关资源
        最近更新 更多