Black Background for XAML Editor

后端 未结 5 1333
孤独总比滥情好
孤独总比滥情好 2021-02-04 07:56

I am currently working on a user control that has white text and a transparent background. Unfortunately because the XAML design view within VS2010 has a white background I cann

5条回答
  •  悲&欢浪女
    2021-02-04 08:23

    In your XAML, set your background to black. Then in your user control, use the DesignerProperties to set the background at runtime:

    XAML

    
    

    Code Behind

    public YourUserControl()
    {
      InitializeComponent();
    
      if( !System.ComponentModel.DesignerProperties.GetIsInDesignMode( this ) )
      {
        this.Background = Brushes.Transparent;
      }
    
    }
    



    Alternate Method

    UserControl:

    In your user control, do not declare a background color:

    
    

    UserControl Code Behind:

    In your user control's constructor, use the DesignTime method as above, but check to see if it is Design Mode (opposite check from other method):

    public YourUserControl()
    {
      InitializeComponent();
    
      if( System.ComponentModel.DesignerProperties.GetIsInDesignMode( this ) )
      {
        this.Background = Brushes.Black;
      }
    
    }
    

    App.xaml:

    Finally, in your App.xaml, add a style to set a background color for UserControls:

    
      
    
    

    Here's what's happening:

    1. The App.xaml will effect the UserControl at design time because a typed style is applied on an object automatically, but it is not applied to a derived object (UserControl in this case). So, at design time, VS thinks it should apply the style, but at runtime, it will be ignored.
    2. The GetIsInDesignMode check will effect the UserControl when viewing the control in a Window that is using the UserControl because VS is compiling the UserControl at design time in order to render it in the Visual Designer.

    HTH's

提交回复
热议问题