Visual Studios C# switching between grid controls - c#

I have begun windows programming and have just completed a functional calculator. I would like to take this calculator further by adding a "Fnc" or "function" button to the user controls which would allow you to view a new set of buttons, but still see the text box with the current entries. I am not sure of how I would complete this using Visual Studios Desktop Edition with Xaml and C#. Could I please get some help?

One possible way is by adding another <Grid> -or any other container control you see it suitable: StackPanel, WrapPanel, DockPanel, you name it- containing additional buttons. Then you can simply switch the <Grid>'s Visibility between Visible and Collapsed upon "function" button clicked.

Related

C#, WPF, MVVM, Creating a viewmodel for message dialogs/confirmation dialogs?

I am not using anything other than a simple WPF application project in visual studio. I've implemented an mvvm application.
I want to display a list of content changes made by a user. I have a main window view model and it currently just builds a strings with changes. I have objects that I can reuse to display their properties (the content).
Currently, I use a MessageBoxResult to show a really long string with the changes. This is a terrible design (I know), but I couldn't really find an answer to what class a regular wpf project has that would allow me to achieve what I want.
I know there is a popup class I can use. In practice, which is better-- another view model for the dialog, or a popup?
Can anyone provide a simple example of one of the two approaches?
Thank you in advance for your response.
What I've done in the past is have a simple Border control, and inside of a TextBlock and whatever Button controls I need. I bind the TextBlock.Text to a public string property named "MessageBoxMessage" which calls OnPropertyChanged(). I bind the Command of each Button to a separate public ICommand which specifies what action to take in the view model when the button is clicked. I then bind the visibility of the Border control - which contains all of the other controls I mentioned - to a Visibility property.
When I want to show a dialog, I set the MessageBoxMessage to the message I want to show, makes sure the commands are set properly, and then set the Visibility on the Border to Visibility.Visible. This shows the box (border), message, and buttons.
You can even implement a semi-transparent rectangle underneath the border (over the rest of the form) that you set to visible at the same time. This will give you the nice "form dimmed" effect and also block the normal form controls from being clicked. A general note - for this to work, these controls need to be at the very bottom of your XAML as the z-index among controls at the same level is inferred from their placement in the XAML - lower in the code is top level on the form.
Let me know if you have any questions about implementing this if it sounds like what you are looking for.

Using a DatePicker without TextBox (Silverlight Toolkit) WP7

I am developing an app for Windows Phone 7.8 (probably WP8 at a later date) that needs the user to enter a date. I am currently using the DatePicker control from the Silverlight Toolkit as I like the visual style of it.
The DatePicker control, when placed on the page, is shown as a TextBox that when clicked opens the DatePicker.
Is there anyway to not have the TextBox included as I do not need and it is cluttering up my form. Maybe a way to open the DatePicker through code rather than trough the OnClick event?
Any help is appreciated :)
You can simply edit the Control Template of the DatePicker to change the way it looks.
VS 2012 can do this by default (right click and edit template), else you will need to edit the template using Expression Blend.
In particular, the Control Template consists of a Button (among other things):
<Button x:Name="DateTimeButton"
Content="{TemplateBinding ValueString}"
...
The Buttons Content property is bound to ValueString. If you remove this Binding (for example) you will be left with a blank button. You could put anything you like in the content of the button...
These links should help you get started with using Blend:
Styling a control that supports templates
Create or modify a template
The guides on Project Rosetta are nice too.
may be, you can create the DatePicker and hiden it.
when you click item (item which you want to use), you change focus to DatePicker.
I think that you can instantiate the DatePickerPage object and navigate to it. As I understand it has a Value property (coming from the IDateTimePickerPage interface) which you can set and read.
I cannot test it at today but I will definitely try it tomorrow and let you know what I found. Meantime you can go to the http://silverlight.codeplex.com/ page and check the source of the Toolkit.
What is the event that should trigger the opening of the datepicker page? Is it a click or tap on a button, an image or what?
If it is any of these, you could place a DatePicker in xaml over that control so that they overlap (placing them in a grid's same row, same column). Set the Opacity of the DatePicker to 0. Then the user will not see the DatePicker's TextBox, just the control below it. However, when the user clicks on that control (button, image, etc), he or she will in fact click on the invisible DatePicker and then the datepicker page will open up.

Create Simple Custom XAML / C# button

First of all, I'm new to XAML / C# (am iOS/Android programmer) so please bear with me instead of immediately voting down. My app has some GridViews that contain buttons. A lot of these buttons are similar and I would like to refactor this. For simplicity let's say my buttons are just Rectangles with a given color. This color might come from the Item class that defines the particular item in the GridView, or it might be hardcoded. I want the rectangle to change color on hover and pressed states. I want these colors to be parameters as well.
What is the best way to achieve this?
I tried to make a Button subclass but somehow I couldn't access the dependency properties in the VisualStateManager
I tried to write stuff in the code-behind but then I wasn't sure how to delegate the click command to the ViewModel class.
Could someone give me a small working example?
Thanks
You can do this with style templates.
In the Visual Studio designer, right-click on your button and then select Edit Template and then select Edit a Copy....
You will then be prompted to name your new style and also for which file to store it in. For now, just give it a unique name such as MyButtonStyle, and select the current file.
Visual Studio will then add a copy of the style to the current xaml document, and will update your button to use the new style.
<Button x:Name="Download" Style="{StaticResource MyButtonStyle}"></Button>
After this, you can update the new style including changing the colors for the different visual states such as hover or clicked.
You can then proceed to use the new style in other buttons in the same document. To use the style in multiple xaml documents, you have to pull it out into a common resource file.
So you want to adjust your button using custom properties. This is a good time to use a custom control. You can create whatever dependency properties you want and adjust your layout in your code. http://timheuer.com/blog/archive/2012/03/07/creating-custom-controls-for-metro-style-apps.aspx

Creating a reusable interface element in WPF

I'm developing a WPF application in C# and was thinking about implementing a custom UI element accross various windows.
I would like to have a minimized tray (only about 4px visible) that expands after clicking on an icon next to the tray. The expanded version would show all controls and would minimize when I click the icon again. I created a quick HTML concept to clarify things.
I know I could put a stackpanel and button in my application and making both of them move up when I click the button, but then I would need to duplicate the code a lot.
Though I'm experienced with C#, I'm fairly new to WPF interface development/templates, but I'm sure there has to be a way so I can use that UI element accross my application without needing to copy/paste a lot of lines of code in my form class file.
I hope someone can help me, or at least point me in the right direction.
There are three ways to customize your elements.
1 If you only need visual modifications you can use styles to change the appearance of the .net default controls. You can even override / extend the default templates.
2 If you want custom logic in a control you can create a custom control. The framework brings a lot of "primitives" to build upon. Examples are ContentControl or HeaderedContentControl. Say you want to build a custom expander control you can inherit your custom control from HeaderedContentControl which provides you with Header and Content properties and you just have to implement the toggling logic yourself.
CustomControls are a good choice if you want to build basic functionality which can be used throughout your application. They can be themed/styled depending on the use case, too (see 1).
3 If you want to compose different controls into one control you can create a UserControl. User controls are composed using XAML. Most top level controls are user controls driven by a view model.
Your case can be build using a Popup and ToggleButton or an Expander.
The decision depends on the desired behavior. If you want the opened panel to move following content down you need a expander. If you want a dropdown like functionality you need popup.
If you use a popup just bind the IsPopupOpen Property to IsChecked of the ToggleButton and set PopupStaysOpen = false to wire the button to your popup.
If you use an expander control you should create a style which can be applied to all equal expanders in your application to minimize the required XAML in each view.
How about using Expander Control?
There's a control called an Expander that is perfect for this. You'll have to style it to look like you want, however it has the functionality you want built-in.

Managing Lots of Overlapping Controls in Visual Studio

I'm using different sets of controls on the same location on a form. By default all are visible=false and then certain subsets of the controls are set to visible as the user selects specific values in a combobox dropdown control.
From the user's perspective this works well since they only see the controls that are needed.
However, since the controls occupy the same location on the form it is difficult to manage these in Visual Studio design view.
Is there a way to group sets of these overlapping controls in Visual Studio so that I can select the entire subset of controls quickly and easily? Is there a way to hide certain controls in design view? Right now everything is stacked on top of each other when developing so it makes managing these controls difficult.
To get such a beast to work i would put every group into it's own UserControl. On your MainForm you stack all these UserControls above each other.
So at the MainForm you can't really get a good overview, but now you got for every group your individual designer view and in your main form you can hide the complete group by a single line of code userControl.Visible = false.
A TabControl can do this, works well in design mode. You just need to hide the tabs at runtime. Check my code in this thread.
You can not hide them.
However you can group them in group box
and using "Bring to front" and "Send to back" property deal with them.
First of all,
If you work with multiple components in same location, you can use groupboxes in your form. Then, to superimpose these groupboxes, you should edit each of your groupboxes on different place in your form screen. After the edit, you should input size and location data manually in your groupbox properties menu.
If you want to edit one of your groupbox after the set location, you can easily right click any of your groupboxes then click "send to back" and "bring in front" commands. I hope it helps.

Categories