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
Related
How i can make this buttons with icons like these ??
Normally, in order to create stylized buttons in Winforms, you have to work with the Button.Image property:
this.myButton.FlatStyle = FlatStyle.Flat;
this.myButton.Image = // your image
// ...
The buttons in your image include a text, but since the enumeration System.Windows.Forms.TextImageRelation (used in the property Button.TextImageRelation) does not allow for a text below the image... obtaining the same style with this approach risks to become a hard task (using GDI, handling painting events, creating a derivative of the Button class, etc...), unless there is a trick that I don't know.
The fastest and simplest way to recreate the same style is to create a button with empty Text property and manually include the text in the image file. Actually, you set:
this.myButton.FlatStyle = FlatStyle.Flat;
this.myButton.ImageAlign = ContentAlignment.MiddleCenter;
this.myButton.Text = "";
and you put this image (just an example) straight into the control:
I'm a university student therefore I'm not sure on everything to do with writing code. If you could provide hints or a bit of help. I have hidden the listbox via the designer. I've tried listbox1.Show under next button event handler. I've tried looking around on the web but I'm getting no where.
Now answered. Thank you
The solution depends on how you've hidden your listbox. If you did set visible property to false, just use listbox1.Visible = true;.
If you used ' Send to back' to hide it behind another control, you can use listbox1.BringToFront(); to set it into the foreground.
See https://msdn.microsoft.com/en-gb/library/system.windows.forms.control.visible.aspx and https://msdn.microsoft.com/en-gb/library/system.windows.forms.control.bringtofront.aspx
Inside Button_Click Event write:
listbox1.Visible = true;
In my opinion, the best way to show/hide controls (in WPF) is to collapse them. This allows the rest of the controls to behave as if the collapsed control does not even exists, until it is made visible, of course.
This would be done like so:
control1.Visibility = Visibility.Collapsed;
control1.Visibility = Visibility.Visible;
If you are using WinForms, controls will not have a collapse option, and the correct way would be as Almansour has said.
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.
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);
I've seen two threads here about TDI & C#. Both of them didn't really answer the questions I have ...
Since TDIs are pretty much like a standard nowadays, I can hardly imagine, that I have to buy a special control (like AvalonDock or SandDock).
This must be possible with built in the tab-control(?) somehow! I don't need special features like dock- and draggable tabitems. Just open every form in a new tab. Thats it.
Like putting every forms content controls into user controls and by request (button, menu click ...) add a new tab and put the corresponding user control on it ... something like this.
How would you do it? This can't be THAT complicated (even for me) or am I missing something?!
thanks a lot!
Maybe Josh Smith's article on MVVM can give you an idea how to design such user interface. Example being built there is kinda tabbed document interface so you can use it as a starting block.
It's not that hard. It seems hard because there are a lot of different ways to do it.
Try this:
<TabControl x:Name="documentArea"/>
Handler for AddForm button:
private void AddFormClick(object sender, RoutedEventArgs e)
{
object form = GetNewForm();
documentArea.Items.Add(form);
}
That's it. You have to implement GetNewForm() in one of two ways. Have it return a user control that displays the form.
OR better yet, have it return your document that you want to display. Use a DataTemplate to select the controls to use for displaying this document. This method is going to be more complex to set up.