Joe.Net
Joe.Net

Reputation: 1215

WPF Transparency and Switching Styles between Transparent and Non-Transparent

2 Questions :

  1. Firstly:
    Is it possible to Toggle Transparency on a WPF window? Any pointers greatly appreciated!
  2. Secondly:
    Most controls on my window inherit their Transparancy from the parent window, however I have a Datagrid control with its own style - The style is in an external file that I reference (Style="{DynamicResource MyDGStyle}")..... in the xaml code behind can I switch Styles? (Ideally I would achieve this using a Style Trigger, but don't think I can).

Thanks very much

Joe

Edit (can't seem to reply)

Thanks alex, NVM

Regarding the Toggling Transparency, as long as I can set the 'Background' property of the Window at runtime from a color to 'Transparent' at that runtime, thats fine.

Regarding switching styles, just extending your code alex, presumably I can do something like

void OnButtonPress()
{
     var transparentStyle = Themes.CurrentTheme.MyDGNonTransparentStyle;
     var nonTransparentStyle = Themes.CurrentTheme.MyDGNonTransparentStyle;

     if (isTransparent) // Change to Non-Transparent
         this.MyGrid.Style = (Style)this.FindResource(nonTransparentStyle); 
     else // Change to Transparent
         this.MyGrid.Style = (Style)this.FindResource(nonTransparentStyle); 
}

?

Thanks

Joe

3rd Edit

Thanks guys,

Sorry to confuse you - my second question was since my datagrid has its own style (and doesn't inherit from the window) I will need to set its style depending on the current state (Transparent / Non-ransparent) - so I need to change the datagrid style at runtime - now since this can be done with a window, can I assume it can be done with a datagrid?

Thanks

Joe

Upvotes: 0

Views: 1912

Answers (1)

Alex Zhevzhik
Alex Zhevzhik

Reputation: 3397

Is it possible to Toggle Transparency on a WPF window?

Yes, it is:

<Window WindowStyle="None" 
        AllowsTransparency="True"
        Background="#88aa3366">
</Window/>

The bad news is that you have to implement the logic of window header by yourself. This article might be helpfull.

in the xaml code behind can I switch Styles?

The question is a little bit unclear, maybe this helps:

var key = Themes.CurrentTheme.MyDGStyle;
this.MyGrid.Style = (Style)this.FindResource(key);

Upvotes: 2

Related Questions