WinForms Inheritance - winforms

I have been using form inheritance for a while now but without doing much research with the following approach. Simply create a new class instead of a form and inherit from an existing form and convert required controls to protected as needed. Visual Studio 2010 designer works like a charm. If more control is required, you can always override base methods.
I am now creating generic forms as follows:
partial class EntityCollectionEditor < T > : Form where T : ISomeInterface < T >
Forms such as this when inherited by simple non-designer classes give the following error:
The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file: XYZ. The base class EntityCollectionEditor could not be loaded. Ensure the assembly has been referenced and that all projects have been built.
A quick solution would be nice but I'm also looking for a good resource/article to educate myself.

Currently, the winforms designer does not support generic forms/controls.
The only work around I have used is to create a specific form type:
class GenericBaseForm<T> : Form
{ }
class IntForm : GenericBaseForm<Int>
{ }
class StringForm : GenericBaseForm<String>
{ }
The specific forms can now be used in the designer. Unfortunatly, if you have a lot of specific forms, its probably not an ideal solution.

Related

WPF: Multiple XAML files that share the same code behind file

I want to create an application where the user navigates through pages that are placed inside a frame element. The problem is that one page can have different layouts which basically provide the same functionality. There can be a few buttons or input controls more or less per layout, but they all should share the same code behind file.
In Windows Forms, I used to place all elements (the layout) on the same form and then hide/show the controls I required, but that's a very ugly solution and I was hoping that WPF provided something more convenient here.
I tried to create 2 Pages, deleted their respective .cs files and set their "x:Class" attribute to a custom .cs file, but that results in compiler errors (ambiguous calls to InitializeComponent() ).
So can I have multiple pages that share the same code?
You should move the logic from the code-behind class to a view model class. This pattern is known as Model-View-ViewModel and is the recommended design pattern to use when developing XAML based user interface applications.
There are plenty of online tutorials about it and this one should provide a good starting point for you: https://msdn.microsoft.com/en-us/library/hh848246.aspx.
Once you have understood the pattern and implemented your application logic in a view model class, you could then simply set the DataContext property of both pages to the same view model:
public Page1()
{
InitializeComponent();
DataContext = new ViewModel();
}
A code-behind class is simply a partial class, i.e. it is partial definition of the Page that you define in XAML and therefore you cannot "share" this one between several different pages.
Partial classes are just a way of splitting the definition of a class across several different source files: https://learn.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/classes-and-structs/partial-classes-and-methods.

StyleTypedProperty Attribute not working in WinRT designer for custom control

I have created my own custom control in WinRT, which has own template for it. My custom control contains many inner custom elements(Small Controls) which are also having own templates.
Then i tried to provided the designer edit facility in Visual Studio for all that templates by StyleTypedProperty attribute and it results fail to me. In designer,EditTemplate working fine for my Control but EditAdditionalTemplates does not contain the templates of inner elements of my control.
NOTE: Same working fine in WPF designer.
Any one faced this kind of issue or please provide some suggestions?
[StyleTypedProperty(Property = "PopupStyle", StyleTargetType = typeof(Popup))]
public class MyControl : Control
{
}

How do inherit in Wpf that i do in windows form

in windows form i create a base form and inherit all my forms from that base one .
then with this way i can share all my property , function and variable that i want be in all forms . also this object oriented way help me to change all form fast with just change my base form
now i want know how can i do some thing like this in WPF
i handle share function with a public static class but i search for better way ...
You can use a class without design mode (xaml).
check this out
site reference

Navigating from one child page to another in Silverlight

We are using Silverlight MVVM pattern in our application. In the application there is a mainpage (which does not change) and there are child pages, these child pages changes depending on the operation performed by the user. Untill now I have been using code-behind for navigation between different child pages, the code goes like this :
ChildPage2 obj = new ChildPage2 ();
Dialog_Box.Children.Clear();
Dialog_Box.Visibility = Visibility.Visible;
Dialog_Box.Children.Add(obj );
But as I am using MVVM pattern I want to do the same using my ViewModel. Is there a way to do the same (Navigation) using ViewModels.
Please help, thanks in advance.
Vaibhav
Couple of basic rules:
ViewModels should not know about how they are displayed. They are purely glue between views and real data objects & business logic.
Views only know how to display data with a certain shape. They should not know where the data is coming from (the exception that breaks this rule is using DomainDataSources... but that's another story).
Look at the navigation features available in Silverlight (try creating a sample Business application in Visual Studio). Your views are then created when hyperlinks are clicked based on configured mappings.
The alternative (to do it in code) is to introduce controllers into MVVM. This maintains the separation of concerns between views, viewmodels and data, but adds a level of complexity I usually reserve for PRISM-based apps. Best you try the hyperlink/url mapping option.
The way I solved this before was to have properties in the ViewModel that each child page would bind to:
public class YourViewModel : INotifyPropertyChanged
{
public Visibility FooVisibility { get { /* ... */ } }
public Visibility BarVisibility { get { /* ... */ } }
}

WPF : Definition Control Template by programmatically

We can make WPF Custom Control for overriding some control's look&feel, like icon button.
public class MyButton : Button {
static MyButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyButton), new FrameworkPropertyMetadata(typeof(MyButton)));
}
...
}
But I think that way has a some problem. It causes a some issue when I distribute the Custom Cotrol 'MyButton'. Because MyButton dependent on external resource, the WPF Style MyButton. So I need to distribute two files : MyButton.cs and MyButton.WPF.
So, How can I definite a Conotrol Template by programmatically?
(Of cause, another way to solving the problem is making WPF User Control. but my point is not that.)
Note : I found some resources about this issue. That was a Inline XAML scripting. But to me, the XAML scripting is not option. Because I'm learning on WPF so I want to know WPF thatself, not a trick.
You do not distribute the code, you distribute a dll that contains the class and the generic.xaml
That way another designer/developer can 'override' the template and your template stays as a safe fall-back.
EDIT
Defining a Template in code is no fun (a lot of code, hard to debug, hard to maintain) but it can be done:
var template = new ControlTemplate(typeof(MyControl));
EDIT2
Another hack is to specify the template in a long string and use the XAML Parser to load it.

Resources