WPF Window Inheritance - c#

I have an application that will run in two modes, each with very similar displays. The application is supposed to allow easy modification of a user interface. One of the features is that it has to display the user interface. Both of these windows look the same, just one has more menus than the other.
I'd like to just create a base template (the user visual) and then inherit it for the editor. That way if one interface changes, both of them change. But this doesn't seem to be possible using WPF. I try to inherit and I get warnings about hiding members. I also don't see how I'm going to append new menus to the base template.
Is what I'm trying to do possible? Is there a better way that I'm supposed to be doing this? It seems like I'm fighting the way that they want me to make the application.

If I got your question properly, all you have to do is create a style for a window in shared resources. When user changes a style it will be automatically applied to every control that uses this style.
For the custom parts you could use ContentPresenters and custom controls..

Related

Cleanest Way to Have Multiple Views/Tabs Inside Single Form/Window

My goal is to make an application where, conceptually, everything happens in a single window - a principle most applications I commonly use exhibit, at least for their main interface. Installers are a good example of what I am trying to do, where you basically page through the interface.
You can do this fairly well using a (invisible) tabcontrol. This allows you to design the individual tabpages (which are basically forms as far as controls go) in the designer, and you can manually switch between tabpages, and it all takes place inside the same outer form.
The problem is, this results in code for everything all in the main form. What I want is basically the design capabilities of separate forms (drag-and-drop components, only has code for its own controls), but that can be put inside the tabcontrol to get the intended user interface.
What is the best way to do this? I assume there is a clean solution given how common this is.
Thus far I have followed C# Multiple Screen View Single Form, which works well aside from the fact that all the code ends up in the same class (for example, you need unique names for every single element on the form).
I think you're looking for UserControls. They let you create a "form without a window" if I had to simplify it.
Once you've built the UserControl, you can then drag and drop it into any Form.
You can then replace these controls at runtime as needed.
See this link for a decent starting tutorial. I'll update if I find a better tutorial out there.
http://www.akadia.com/services/dotnet_user_controls.html

Developing a form designer for a scripting language, how to correctly handle the custom classes

I want to develop a custom form designer for a scripting language. The controls' properties and behavior are very very similar to the windows forms controls.
I know that design such application is not an easy task and develop it from scratch is a little to advanced for me. So I googled for it and found lots of examples in c# for custom forms designers.
I picked this one to work on top of it:
https://msdn.microsoft.com/en-us/magazine/cc163634.aspx
Most of the windows controls properties won't be used, and some will be new. Let's take a button as example, I won't need many propertie like AllowDrop, Tag, etc. Some will be new, like ControllerType and BehaviorType. Others are preety much the same but with other name, like Location which will turn to be Position.
The best approach I could think of is to create a new class derived from Button and create these new properties on this class.
I managed to implement all this but now I have a problem with the PropertyGrid. Since I'm not using most base class properties, I would like to hide them on the PropertyGrid, and if possible, rename the properties that have a diferent name. I cannot control the attributes of the base class to set [Browsable(false)].
So my questions are:
1) Is this approach I'm following correct, or is there a better one?
2) Can I control the PropertyGrid behavior like what I need or do I need to develop a custom propertygrid?
3) If I can't run away from creating a custom PropertyGrid, could someone hint me on how to do this?

Why do we use UserControl?

ı have been serching this for a while but I couldn't come up with a conclusion. What is UserControl? For me we can do everything with creating new windows forms instead of User Control. I know there is a reason to use but it is not clear right now. If someone illuminates the mystery that would be great.
A user control is basically a grouping of other existing control, intended as a reusable component (i.e. composite control). If you need to place the same group of controls on different windows you'd rather group them in a user control, adding things like data validation for instance, and then reuse this control whenever you need it.
Here is some more reading.
UserControls allow you to reuse your code. For example if you need a small component that displays two values (code and description), with UserControls you can design it only one time and then reuse it in other forms.
Also, you can add your custom properties\methods to the UserControl; in this way you can define simple (or even more complex) functions associated to the GUI control.
Hope this helps.
imagine you have a GridView with some new methods you create, and which you want to use on several pages. There you go. A UserControl is useful. That's just one example
As the others have explained a UserControl groups 'real' Controls and the logic that makes them work together as one component.
Imagine an application where the user can decide wether it runs in MDI mode or with separate windows or with tabbed pages. You can add the UCs of your application to any of these easily.
Think of a MP3 player with various controls, buttons, labels and sliders and user drawn-gauges. If it's in a UC you can re-use it directly. If it is all on a window, how do you re-use it?
So UCs are about flexibilty and re-using visual components.

C# Multi-panel/layer winform application

I've been designing a pretty complicated avionics application. The thing is, it has many menu buttons to be clicked (12 to be exact) and each one of them perform a different action. For instance, one could be a login panel and the other one a PDF reader. How could I organize this programmatically?
Currently, I've been setting each item in a panel and setting it to visible or invisible, according to the active or clicked item.
How would you guys do this?
Thanks in advance!
You might consider a FlowLayoutPanel, although I'm not sure how flexible it would be in meeting your requirements. If you set your panels up with docking properties, you should be able to manage.
I would also recommend using a UserControl to separate code and functionality. If panels need to communicate, implement the observer/observable pattern instead of subscribing to events between user controls.
Like IAbstract says, you should consider separating the different UI elements as UserControls. You can then do things like use a factory to construct them and add them to your window as required.
I've found this sort of approach, used with a Model-View-Presenter type pattern, works really well for WinForms apps with dynamic user interfaces.

Use Dependency Properties for global properties

I am new to WPF. I have been reading a lot about WPF and dependency properties. My understanding of Dependency Properties is to bind properties to XAML (XAML being the target). I have envisioned a program in which a single configuration window would control all visual elements of the rest of the program (font, font size, colors, etc).
In my first attempt, I created a separate window that would contain all the different configuration options. This failed because I was unable to figure out how to use the dependency properties in separate XAML files.
Also, the dependency properties can only be registered in a class that implements directly or indirectly the DependecyObject class. How do I know which wpf objects implement it? It appears the window does not, but I could be wrong. My second attempt was this, to register the dependencies in the top window, so the whole visual tree would have access to it. I had exceptions being thrown and the only thing I could think of was that the window does not implement to DependencyObject. (I could be mistaken, and the visual tree thing is still sort of a mystery to me. I am learning though)
So now I'm at a loss. Is what I am attempting even possible? The whole purpose of this project was to help me learn WPF and now I'm wondering if I should lower my expectations of what I can accomplish with WPF.
Edit:
What kind of exception? What are you trying to do? Could you paste some code?
public partial class MainWindow : Window {
public Color BackColor {
get { return (Color)GetValue(BackColorProperty); }
set { SetValue(BackColorProperty, value); }
}
public static readonly DependencyProperty BackColorProperty =
DependencyProperty.Register("BackColor",typeof(Color),typeof(MainWindow),
new UIPropertyMetadata(0));
It appears that Color was causing the exception. I changed it to int and no exception was thrown. So are primitive types only able to be registered?
WPF is a framework to help you bulit really good looking apps (it includes also Printing etc.) What are you trying to do? Should it be a part of Visual Tree?
Again, this project was only meant to get me to learn WPF. My idea was to create a main UI with different pages. A configuration button would bring up a window with options to change font, fontsize, background color, etc. These changes would be applied in the main UI as the user made the changes. My issue has been how do I access those dependency properties outside of the configuration class where they were registered? How do I have one window that can have slider and combo boxes, and any other class can access those configuration settings?
Put simply, I would like that a single configuration (fonts and colors) be somehow inherited by all UIs. And that that configuration can be changed and seen by the user.
Also, the dependency properties can only be registered in a class that implements directly or indeirectly the DependecyObject class. How do I know which wpf objects implement it? It appears the window does not, but I could be wrong.
http://msdn.microsoft.com/en-us/library/system.windows.window.aspx
look at the Inheritance Hierarchy it does implement DependecyObject
My second attempt was this, to register the dependencies in the top window, so the whole visual tree would have access to it. I had exceptions being thrown and the only thing I could thing of was that the window does not implement to DependencyObject.
What kind of exception? What are you trying to do? Could you paste some code?
So now I'm at a loss. Is what I am attempting even possible? The whole purpose of this project was to help me learn wpf and now I'm wondering if I should lower my expectations of what I can accomplish with wpf.
WPF is a framework to help you bulit really good looking apps (it includes also Printing etc.) What are you trying to do? Should it be a part of Visual Tree?
We cannot tackle here the big task of learning a whole new framework and designing your whole program. That will take time. It can be daunting but stick with it.
As for your exception, the problem is that the mysterious:
UIPropertyMetadata(0)
is actually providing the default value for the dependency property and this value must match the type specified in the second argument to Register. Since 0 is an integer and your property is a Color the dependency property subsystem throws an exception. Instead you can use:
UIPropertyMetadata(new Color())
or any other color as the default.
Dependency properties are a newer design specifically created for WPF. One of the problems they solve is to create faster resolves of the property values because they don't use older, slower, techniques (Read up on why they were created).
They are hard at first, to understand, because it's not exactly clear why they are needed when there is support for the Interface named INotifyPropertyChanged. But one main reason (among others) is that if you implement a DependencyProperty correctly, you will be able to change the design time property values of that property in the property window! Now this is a very cool thing to do because you can very easily set all of your default properties that way.
Doing this for the sake of design time property editing allows you to learn a lot more about DPs and why they are used. In fact, some prefer them everywhere, as they just are not that hard to code up.
DPs are also used when creating custom controls using the "Generic" folder method. This is how Microsoft themselves create all their controls.
There are some caveats to be aware of when using them. You cannot see the design time properties of a DPs until that particular control is contained by something else. In other words a UserControl with DPs will not show those Dps in the UserControl design. They will; however, show up in the control that contains that user control.
DPs bring maximum control using Metadata, PropertyChanged callbacks, default values, and Cohersion and other techniques which give you 100% control of what they contain. The DP is the ultimate in property control.
Keep scouring the internet as there are tons of articles out there on the topic.

Categories

Resources