WPF Transparency and Switching Styles between Transparent and Non-Transparent - c#

2 Questions :
Firstly:
Is it possible to Toggle Transparency on a WPF window? Any pointers greatly appreciated!
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

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);

Related

Setting ContextMenu Style to 2013

I'm trying to change the default style of the contextmenu to 2013/2015 in my rehosted vs13 application.
The problem occurs in only one designer, everywhere else its the correct one. I've tried to override both the XAML code and the code behind, checked if something else was changing the style, but without anykind of result.
Is there even a way to change the default style? Am I overseeing something?
Okay, after some heavy research/try and error if finally found out what was wrong: I couldnt access the control I wanted to change the ordinary way, so I had to think outside of the box (and ask a collegue for help).
This is the code that works for me, its not pretty, but it deletes the 'standard'-style set by WPF.
var dv = wd.Context.Services.GetService<DesignerView>();
dv.MenuItemStyle = null;
dv.MenuSeparatorStyle = null;
dv.Resources[typeof(ContextMenu)] = new Style(typeof(ContextMenu));
Quick thanks to Glen Thomas for trying to help.

How to change the background image of a button when clicked in code?

There is a similar question like mine here in Stackoverflow but it only explains how to change it in XAML. I want to know how can I change it in code.
Here is a image that shows how I do it in XAML using Blend:
Link for full size: https://snag.gy/4Skk4.jpg
Basically I want to change the background of a button's pressed state in C# but I can't seem to find any examples on the Internet. It must be in code because sometimes the image of the button will change therefore the button's pressed image must change as well.
The following code is just to change the image of the button and it's just the start.
image.ImageSource = new System.Windows.Media.Imaging.BitmapImage(new Uri(#"images/Button-warning-icon.png", UriKind.Relative));
image.Stretch = Stretch.Uniform;
buttonWarnings.Background = image;
If I understand you correctly, you are trying to change the appearance of the Button control in a "pressed" visual state.
I'm not near my dev computer to try it out, but to "unblock you" I'll give a direction.
First, as you noticed in your Blend screenshot, each visual state is represented with a Storyboard, which defines how various properties change. In your case, you're looking to change Background property.
The VisualStateGroups and their states are defined by the control. You can override them when you re-template the control. So, retemplate the button control using Blend with "Edit Template"->"Edit Copy".
Then, in code, you should be able to do the following:
1) Get visual states (this would not work unless you re-template the control, AFAIK)
var visualStateGroups = VisualStateManager.GetVisualStateGroups(buttonWarnings);
2) Get the VisualStateGroup of "CommonStates" from the visualStateGroups
collection
var commonStatesGroup = visualStateGroups.Find((g) => ((VisualStateGroup)g).Name == "CommonStates") as VisualStateGroup;
3) Get the "Pressed" VisualState:
var pressedVisualState = commonStatesGroup.Find((vs) => ((VisualState)vs).Name == "Pressed") as VisualState;
4) Change the storyboard of that state
pressedVisualState.Storyboard = newStoryboardWithCustomImageBackgroundProperty;
(Disclaimer: I'm not near in a computer to try it now - it's all in theory)
There are many examples to be found on the internet!
Take a look at some:
http://mobile.dzone.com/articles/windows-phone-buttonimage
http://loekvandenouweland.com/index.php/2011/01/windows-phone-image-button/
Actually its quite simple,
While in button pressed state....see part 3 in the image you uploaded above.
Above all the colors there is a row containing 5 icons.
Click on 4th icon.
it will show you option to choose image as background.

Reset label control in code behind to original XAML?

I have a C# WPF application with a bunch of labels.
When I run my program it does some checks and wether it the check was positive or not it sets it's corrisponding label to green og red.
These changes is done in my .cs file like:
lblCheck14.Foreground = new SolidColorBrush(Colors.Green);
I would like to add a "Reset" button, that reset the application to it's initial start.
How can I easiest implement this?
One way - but I really hope there is a smarter way, is to set them all like:
lblCheck14.Foreground = new SolidColorBrush(Colors.Black);
lblCheck21.Foreground = new SolidColorBrush(Colors.Black);
lblCheck42.Foreground = new SolidColorBrush(Colors.Black);
Etc..
But isn't there a function which I can call that strips away any changes the .cs file have done to the controls in the XAML file? Like make the XAML back to stock?
Sorry for my back explanation. Hope you understand me :)
Best regards
Implement styles. You can have a default style to roll back to when you hit reset.
Take a look at this tutorial if you're unfamiliar with them: http://wpftutorial.net/Styles.html
Do not manipulate UIElements' properties in code. WPF is not winforms. As Yatrix's answer said, implement styles, or even datatemplates and triggers to manipulate different properties of different UIElements acording to some logic (defined in ViewModel or somewhere else). I suggest you to take a look at WPFTutorial.net

How to launch DatePickerPage from ApplicationBarIconButton?

I've tried to launch DatePickerPage after ApplicationBarIconButton is tapped.
I had hope that it would be easy as follows:
DatePickerPage dpp = new DatePickerPage();
dpp.Show();
((ApplicationBarIconButton)ApplicationBar.Buttons[0]).Text = dpp.Value.ToString();
...but it wouldn't.
Could you give me some suggestions, please?
I don't think the DatePickerPage is really intended to be used directly. The key control here is the DatePicker, which itself handles showing the picker page. For detailed information about using the DatePicker control, check out this article on WindowsPhoneGeek.com.
Given that the label of appbar icon buttons is not visible by default, it seems a bit strange to update the label in this way. What are you trying to achieve?
Maku, I'm letting users activate a datepicker from the ApplicationBar by placing it in the page and setting Height/Width to zero and then focusing the DatePicker when the ApplicationBarIcon is pressed.

How to set NotifyIcon behavior to AlwaysShow in C#?

Is it possible to modify NotifyIcon behavior to AlwaysShow in C#? A code snippet would be greatly appreciated:) Thanks in advance...
Cheers
---edited
One of our clients said quote "it seems necessary to customise icons to always show". What he meant was that he has to do it manually by r-clicking on task bar then click on Properties -> Task Bar -> Customize Notifications and then you can set behavior to Always Show / Always Hide / Hide when inactive for each taskbar icon on the list.
Can you do that programically in C#?
I want to ensure that my NotifyIcon is ALWAYS visible. I'm already setting icon.Visible = true but it looks like it doesn't work for him hence the complaint.
Is there any easy way of setting the behavior by altering [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\TrayNotify] IconsStream registry value?
NotifyIcon icon = ...;
icon.Visible = true;
Edit for updated information: There shouldn't be, and if for some reason it exists, don't use it. That's the user's preference, not yours.
I'm sure it's possible with enough Registry hacking, but not at all recommended. They added the collapsing-notification-area behavior in XP because so many applications were shoving themselves in that space. Much like Start Menu pinning behavior in XP/Vista/7, the lack of a public API means you're supposed to let the user decide that sort of thing.

Categories

Resources