I want to have the WPF controls inherited like this,
public class BaseView : UserControl
{
...
}
public class BaseListView : BaseView
{
...
}
public class TeachersListView: BaseListView
{
}
public class StudentsListView : BaseListView
{
}
Here "BaseListView" is the base classed. This classes may have several functionalities which depends on the ListView present in "BaseListView". I want to inherit this "BaseListView" to several views which may add several column's and with different databindings. So my requirement is
class BaseListView : BaseView
{
This class will have the UI parts like commandStrip and followed by "Empty ListView".
This "ListView" may not hold any columns in it.
}
class StudentsListView: BaseListView
{
In **XAML** part, Columns and its appropriate Data Binding will be added. I need to access the controls in .cs file. so that i can access the controls.
void FindAndHighlightColumn()
{
// get the columns to find and search the list view and highlight.
}
}
How can i achieve this, what is the right way to do this.
I think you want to create a UserControl (Items- or ContentControl) with some basic functionality like FindAndHighlightColumn().
You can create your BaseListView "lookless" and then use ControlTemplates to get several flavours of it (create styles like "StudentListViewStyle" etc., each with an appropriate ControlTemplate). A ControlTemplate is a view, so you can specify different bindings in each template, to access controls by their names you need to define a convention with certain parts named 'PART_XY' etc., look at the standard ProgressBar-Control for an example.
Are you using Model-View-ViewModel architecture? After reading your question, my initial thought is that I would not use inheritance to the extent you are thinking of. Instead, I would consider composition.
For example, could you create a composite StudentsListView by composing a .xaml file of multiple user controls? Maybe a user control to display your commandStrip and another user control to display the appropriate ListView (e.g. StudentListViewUserControl, TeacherListViewUserControl, etc.).
Related
I have an issue, and I don't know if I can do that, or if there is another way to do that. I have an abstract class called "BasePage", it is my .NET Standard library, so I can't access to System.Windows namespace. Its declaration is:
public abstract class BasePage<VR> where VR : new()
EDIT: The class above has a public property to access to VR
So now, for each platform (WPF, Xamarin, etc) I have to create its own implementation of the class begin VR the content control of each platform (UserControl for WPF, for example). I already did it and this is:
public class WindowsBasePage : BasePage<UserControl>
Now, every time I want to create a page to add content to a window, I have to create something like this:
public partial class UserPassPage : WindowsBasePage
{
public UserPassPage()
{
InitializeComponent();
}
}
The problem becomes when I have to set it in the XAML file (the View). I can create it and it builds:
<local:WindowsBasePage x:Class="Bitture.AppManager.Manager.UserPassPage"
[...]
xmlns:local="clr-namespace:Bitture.AppManager.Manager"
mc:Ignorable="d" >
</local:WindowsBasePage>
but I can't add components (like buttons, text, grids, stackpanels, etc). I want to know If I can do it with my current code or there is something I have to change. Because I have to access to the generic type WindowsBasePage that inherits from
class WindowsBasePage : BasePage<UserControl>
I am not sure what you are trying to do here but you can't add UIElements to a custom class of yours that doesn't inherit from any of the common WPF base classes such as for example Panel or ContentControl, and expect it to be rendered as a UserControl or some other WPF control.
This won't work. WPF knows about how to render WPF controls but it doesn't know how to render a custom BasePage<UserControl>.
I am building some applications that use OPC to control some industrial automation. I have decided it might be a good idea to create some custom controls for standard things I will use such as buttons and text boxes. One of my main reasons was that I felt in an ideal pattern the end object i.e. the button is the one who holds information about the OPC item it refers to otherwise in a typical application my Form class gets polluted by tons of variables that are in my mind scoped more global then they should. I started then by using the Tag property of the button but this requires some overhead code that is the same for each instance. I felt like the right thing to do is subclass controls that I would like to use and provide properties to configure each one.
With that context in mind my real question is this. To make it as portable as possible I decided there should a property to define the OPC Group. I declared a property like this
public class OPCButton : Button
{
[Category("OPC")]
public OPCConnectedGroup
{
get { return _OPCGroup; }
set { _OPCGroup = value; }
}
}
This shows up int the property list when I add the control but I am unable to bind this property even though my Form1 contains
public OPCConnectedGroup Connection1 = new OPCConnectedGroup();
I have resolved that the way to solve this is to probably create an additional control like OPCGrp that can be added to a Form then the Controls can reference this. To test I added a property of type Button and sure enough when I added it and browsed to the property it gave me options for all the buttons on the Form. I have no huge problems with this approach I just want to make sure that Im following a prototypical pattern because I will be responsible for maintaining the control library but not always implementing and Im trying to get it down to a 1-2 step process to implement a control.
Thanks
Matt
When defining OPCConnectedGroup you can inherit Component class.
This way if you have a property of type OPCConnectedGroup in your OPCButton, then at design time, you can put instances of OPCConnectedGroup on the form, and then if you choose your OPCButton at designer, that property of type OPCConnectedGroup will show as a drop down list that you can select one of instances that you put on the form for it.
Example:
If I have such MyButton and MyClass:
public class MyButton : Button
{
public MyClass MyClassInstance { get; set; }
}
public class MyClass : Component
{
public string SomeProperty {get;set;}
}
Then you can put some (or one) instance of MyClass on the component tray of the form:
And then if you select MyButton on your form, you can choose one of MyClass instances from in property grid:
I'm writing a wpf program that needs several usercontrols that look exactly the same but perform different functions. With winForms, I could just extend the base control and add the functionality, but as far as I know, it's impossible to inherit xaml files. Is there any way I can have different codebehind classes that all use the same xaml file for their control?
You might be able to do this using MVVM pattern and create multiple VMs which perhaps implement the same interface. The XAML view could then be bound to any of your VMs relying on the fact they share the same interface.
In this approach you'd not use the code behind at all.
One way to do it is by using inheritance. A very simple example would be a UserControl with a single button, that should display different contents in a MessageBox.
We will have something like this:
public abstract class SomeUserControl : UserControl
{
//declared by XAML (can be made public with x:FieldModifier="public")
public Button MyButton;
//code-behind
public SomeUserControl() {
InitializeComponent();
}
}
public class MySpecialControl : SomeUserControl {
public MySpecialControl() {
MyButton.Click += (sender, e) => MessageBox.Show("Bla");
}
}
To use the MySpecialControl, just declare it in XAML like you did with your previous one:
<myNamespace:MySpecialControl />
Note that you can also create an abstract method for the button click, if the variation in behavior is not so big.
With winForms, I could just extend the base control
And one can't do similar in WPF?
Create a custom composite control with a dependency property(ies) which would set the mode of the control to fit its the target consumer's needs.
The control could be based on an existing control or controls.
In my asp.net 4.0 application I have a standard set of tabs controls across the application that has one tab for Search, one for View and one for Reporting. I am in the process of updating those tabs and I was hoping to be able to wrap their functionality up into a single user control since they all function in pretty much similar ways. The issue I am having is how to create a property called SeacrhTab/ViewTab/ReportTab that allows me to assign a different user control to it depending on the area of the application (e.g. CompanySearchControl, ContactSearchControl, etc.) Anyone ever tried to accomplish something similar? How should I approach this?
You should just be able to create a regular property in the partial class definition in your code behind page. Make it public and of the type of generic class that includes all tab controls and you should be set.
I have not made other controls properties of a user control but I have certainly used all sort of other custom classes as properties of user controls, so I don't see why it wouldn't work.
Assuming CompanySearchControl and ContactSearchControl both inherit from GenericSearchTabControlClass, for instance:
public GenericSearchTabControlClass SearchTab {get;set;}
public GenericViewTabControlClass ViewTab {get;set;}
public GenericReportTabControlClass ReportTab {get;set;}
Create an enum for the specific layout, view or tab definition ie: SearchTab, ViewTab etc. specify that as a Property of your UserControl, then use that propery to create, or hide and show a specific PlaceHolder or Panel depending on the required layout, or functionality.
public enum ControlLayoutEnum
{
SearchTab = 0,
ViewTab = 1,
ReportTab = 2
}
public class TabViewControl: System.Web.UI.UserControl
{
public ControlLayoutEnum ControlLayout { get; set; }
protected override OnInit(object sender)
{
// Create controls required or Hide/Show PlaceHolder or Panel etc
switch (this.ControlLayout)
{
case ControlLayoutEnum.SearchTab: // Create Search Layout
break;
}
}
I have a c# winform that is a template for all the forms in my project.
My problem is how do I setup some properties for the controls that will be added in the inherited forms from the baseform.
For example I want all the textboxes that will be in the forms to have a specific color, or call an extension method.
Right now I tried the simple idea that popped out:
foreach (Control c in Controls)
{
if(c is ComboBox)
{
//do something
}
if(c is TextBox)
{
//do something
}
}
I put this code in the base form load event, but with no luck. I tried changing the modifiers from the inherited form to protected, but with no luck.
Is there any solution to this problem? Or I am obliged to put this code in all of my forms that inherit baseForm?
Custom Controls are the solution to the problem you have at hand. Simply extend existing Controls to have attributes of your desire and then you could these controls in all of your Forms.
You're beginning to think along the right lines, but you're not quite there yet. The solution to this problem is definitely object-oriented inheritance, but you must be careful not to violate other important principles of OOP, namely encapsulation.
To put things a different way, the form should not be required to "know" about the properties of the controls that it contains. It shouldn't know or care that it contains a ComboBox or a TextBox or a ListView.
Instead, you should subclass each of the child controls that you want to modify, and set their default properties there. Then, you would just add an instance of your custom subclassed control to your form, rather than the built-in control.
So, for example, your custom TextBox class might look like this:
public class CustomTextBox : TextBox // inherit from TextBox
{
public CustomTextBox()
{
// default constructor
}
// other logic...
}