Le réseau pour les développeurs > Forums - Accueil > Windows Presentation Foundation (WPF) > Setting the Opacity of a xaml Control to see through it?
Poser une questionPoser une question
 

TraitéeSetting the Opacity of a xaml Control to see through it?

  • lundi 10 décembre 2007 19:15ParaDiddl Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

     

    I'm adding a xaml user control on top of a windows form. Is it possible to set the opacity of the xaml control in such a way that I can see through it and see the windows form below it?

Réponses

  • mercredi 12 décembre 2007 06:59Wei Zhou - MSFTModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée

    Hi ParaDiddl

     

    I think that you can set the BackColor of ElementHost to Color.Transparent, and set opacity on hosted WPF control to achieve this. The following code shows how to do this.

     

    Code Block

    public partial class MainForm : Form

    {

        public MainForm()

        {

            InitializeComponent();

     

            this.BackColor = Color.Blue;

     

            MyWPFUserControl control = new MyWPFUserControl();

            ElementHost host = new ElementHost()

            {

                BackColor = Color.Transparent,

                Dock = DockStyle.Top,

                Height = 100,

                Child = control

            };

            this.Controls.Add(host);

        }

    }

     

    <UserControl x:Class="ForumProjects.MyWPFUserControl"

       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

       Opacity="0.5">

        <StackPanel>

            <Button>Button</Button>

            <TextBlock Height="50" Background="Red">

                This is a text.

            </TextBlock>

        </StackPanel>

    </UserControl>

     

     

    Best Regards,

    Wei Zhou

Toutes les réponses

  • lundi 10 décembre 2007 19:54TomGiam Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

    Here is an example from code:

     

    Rectangle filter = new Rectangle();

    SolidColorBrush TransparentrBrush = new SolidColorBrush(Color.FromRgb(0xA8, 0x83, 0x65));

    TransparentBrush.Opacity = 0;

    filter.Fill = TransparentBrush;

     

    Tom

     

  • lundi 10 décembre 2007 20:35ParaDiddl Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

     

    Can you elaborate more? I need the actual user control to be transparent, not a rectangle within the user control. Setting the opacity property of the user control doesn't work though.

     

     

  • mardi 11 décembre 2007 13:03TomGiam Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

    Sorry, I don't know another way other than iterating through all of the UIElements in the user control and setting each ones opacity property (where available).

     

    Tom

     

  • mardi 11 décembre 2007 14:15aelij Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Windows Forms controls do not support control opacity. When you add a WPF control to a form, you use a winforms control called ElementHost, which behaves like any other winforms control, meaning it can't be transparent to the form/controls below it (opacity inside the WPF control is still supported, of course).

    One option I can think of is to use a top level window to host WPF (e.g. using the WPF Popup class and setting AllowsTransparency to true) and synchronize its position and size with the form. You should seriously consider whether this effect is worth the hassle, though.
  • mardi 11 décembre 2007 20:48ParaDiddl Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

     

     

    That's what I was afraid of since I had no luck in making the control's background transparent.

     

    I think I'd be better off by faking it by making the WPF's background image a screenshot of the form it is covering.

  • mardi 11 décembre 2007 21:56TomGiam Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Depending on how you use the user control, you could replace it with a transparent window with the xaml you need in it.
  • mardi 11 décembre 2007 22:09Jeff Wain Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Do you have any example code you can put up?

     

  • mercredi 12 décembre 2007 06:59Wei Zhou - MSFTModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée

    Hi ParaDiddl

     

    I think that you can set the BackColor of ElementHost to Color.Transparent, and set opacity on hosted WPF control to achieve this. The following code shows how to do this.

     

    Code Block

    public partial class MainForm : Form

    {

        public MainForm()

        {

            InitializeComponent();

     

            this.BackColor = Color.Blue;

     

            MyWPFUserControl control = new MyWPFUserControl();

            ElementHost host = new ElementHost()

            {

                BackColor = Color.Transparent,

                Dock = DockStyle.Top,

                Height = 100,

                Child = control

            };

            this.Controls.Add(host);

        }

    }

     

    <UserControl x:Class="ForumProjects.MyWPFUserControl"

       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

       Opacity="0.5">

        <StackPanel>

            <Button>Button</Button>

            <TextBlock Height="50" Background="Red">

                This is a text.

            </TextBlock>

        </StackPanel>

    </UserControl>

     

     

    Best Regards,

    Wei Zhou

  • mercredi 12 décembre 2007 08:53aelij Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    I'm afraid that won't work. It would allow blending the WPF control with the form's background color, but if you put another control behind the WPF control, you will not see it.

    It's just like when you set a winforms Label control's background to Transparent - you can't get true transparency since Win32 does not support it.
  • mercredi 12 décembre 2007 12:06hypersw Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    And I'm afraid even the background won't be visible on Vista, as Avalon would render with DirectX within its HWND.

     

    It's theoretically possible to have one large Avalon control instead, with WinForms controls added to it as children, having their opacity and taking part in transformations and clippings, and such — at least, for simple cases of controls only. It's that noone has implemented that yet, for all that I know — hosting a HWND/WinForms window first-class in Avalon.
  • mercredi 12 décembre 2007 15:14Bigsby Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

    I don't think I get the issue here...

     

    1. Why not just have a first visual child as a Control with the Opacity property, for instance, a Grid?

    2. Or, the other way around, have the UserControl child to a Control with the Opacity property?

     

    1.

    <UserControl ...>

    <Grid Opacity=".5">

    < ... Windows.Form ... />

    </Grid>

    </UserControl>

     

    2.

    <Grid Opacity=".5">

    <UserControl/>

    </Grid>