Updating a control from another control child - c#

I have some problems trying to update components of surface elements. I dont know if my approach to the problem is wrong, since I'm new to the topic.
My point is represented by the following diagram
According to the option that is selected in the menu, load different user controls as only child of StackPanel but i'm habing problems for update the Listview from loaded user controls, example: when I save a new item I need to recharge the list of items in the ListView

MVVM would be a good pattern here. If you have a problem passing data between controls - why not introduce them on top of unified data layer? Consider this:
Three radio buttons in your Menu, each one's IsChecked property bound to Visibility property of your respective UserControl.
StackPanel holding all three UserControls
ListView bound to ViewModel's List<Item>
Each of your UserControls bound to ListView.SelectedItem: one of them using TextBlock for read-only, one using TextBox for editing. Third one would create new item in your List<Item>. You would have to create ItemTemplate for each or create one UserControl (since they look very much alike) and use DataTemplateSelector.
If you're not familiar with MVVM here is a good start. You can also use one of the existing frameworks like MVVM Light

You can create an event on your child
public delegate void HandleNAMEOFYOURHANDLEEVENT();
on your child class
public event HandleNAMEOFYOURHANDLEEVENT yourInstance;
to use it on your child class
if (!ReferenceEquals(yourInstance, null))
{
yourInstance();
}
and you declare it on your parent like other event.

Related

Contentcontrol or dockpanel for navigation wpf

I use a Contentcontrol to show the user controls of the program, Now there is a problem for me to close the user controls After searching, I found an example that The user controls is loaded on a DockPanel
Now my questions:
What is the difference between these two controls? (Dockpanel vs ContentControl)
Is it okay to use this control(dockpanel) Instead of Contentcontrol to display application user controls?
Is there a similar code for Contentcontrol?
ucChild ChildWindow = new ucChild();
ChildWindow.ParentControl = this.UIPanel;
UIPanel.Children.Clear();
UIPanel.Children.Add(ChildWindow);
Standard disclaimer for people coding WPF like it is WinForms: First off; direct UI manipulation like this is a bad idea. You should
be modifying a view model and allowing the binding system to update
the UI. Use the MVVM pattern; WPF will work for you instead of
against you
To your actual questions:
Everything. I mean; they both inherit from FrameworkElement but that's about it in terms of commonality.
A DockPanel is as the name suggests, a Panel. That is; it controls the layout and sizing of one or more child elements. Specifically, DockPanel is good at situations like the following: you want an element to use up a full column of width, then another span the top (except for the previous element) and have the last element fill the remaining space.
A ContentControl is basically a placeholder, its purpose is to expose a settable (and most importantly, bindable) Content property that you can stuff another control into. Even better; you can put an actual object there and use a DataTemplate to control the display (this approach would conform to MVVM).
You can't really replace one with the other, see above
No. ContentControl is not a Panel and so does not have the Children property.

C# Class that can return or be multiple types of controls

I am building a class that displays either a combobox or a listview (more to follow in the future). Part of the class is a list called OptionList that holds all the values. In the class are methods to update the list and read the list and set the displaystyle (combobox or listview). The List will be bound to either the combobox or the Listview. During runtime the displaystyle can be changed.
I am having a hard time figuring out what the best setup for this would be, should I add a Control member in the class that can be either the listview or the combobox or are there better ways to implement this?
In the calling class I simply want to show the control and update / read the list without worrying what the actual control is
You might be better thinking about the MVC pattern. The "View" would be a wrapper around the particular control you are using. The update methods could move to the controller and the raw data lives in the model.
Support for listview or combobox would be handled by different implementations of your view interface.

class concept in WPF

I'm new in WPF and I need to group many components in one element and make and add new instance of that element in window for each student in database like 2 textblock plus 1 textbox for each student, how can i do something like that?
This is where WPF really shines - you can use an ItemTemplate or a DataTemplate to style the UI with the underlying data objects knowing absolutely nothing about how they are being presented.
Check out Data Templating for an introduction. Effectively an ItemTemplate is a template (definition) of how each item should be rendered. A DataTemplate goes a step further and gives you the ability to select which template to use based on the data item being bound to, so you can have a list containing different types of objects yet still show them all in the same list/repeater control on the screen.

How to change the user control in WPF?

I'm creating something like a wizard, and I'm using several User Control, but the roblem is I need to get the parent from that element to replace for the next user control.
How can I do that?
Lets think you have 5 UserControls. While creating wizard you need to add new UserControl inside a grid and remove the previous UserControl from the same parent grid.
The following function will automatically remove the older UserControl and add the new UserControl.. But for the first UserControl you can directly add it to its parent using MyParentPanel.Children.Add(myFirstUserControl);
private void AddNewUserControlAndAutoRemoveOldUserControl(UserControl control)
{
if (control != null)
{
Panel parent = control.Parent as Panel;
if (parent != null)
{
// Removing old UserControl if present
if(parent.Children.Count > 0)
parent.Children.RemoveAt(0);
parent.Children.Insert(0, control);
}
}
}
}
Hope this helps you!
Well, there are various ways you can accomplish a wizard, but the simplest would be to manage the UserControls from your main form. Just add an area to the main form that will be the parent of each user control and then add/remove a user control from the container when necessary.
The most elegant way to do this (I'd say the best) is using a Selector or a ListBox.
Your wizard will have several pages, each one exposes one or more informations, thus controls bound to some data. In other words, you should consider having a "model" containing the data, where the pages will bind to.
Now, consider having a distinct model for each page, and a list of these models feeding the ListBox. This ListBox should have defined its ItemTemplateSelector, that allows to choose a certain DataTemplate upon the item data (i.e. the model).
The hardest part of this technique is to create/define the Control template for the ListBox, that should be shaped for displaying only the selected item (SelectedItem). In such a way, you only have to change the current selection, and the wizard page will show automatically.
Although this technique appears an overkill, it is dramatically convenient respect to the "classic" approach. Your code is much more clean, easier to debug, reutilizable, and offers high separation between modules. All that will give much more reliability, and fast development.

WinForms UserControl design

I've created user control which hosts datagridview and other controls.
Then I drop it onto a form.
How do I allow myself to customize grid's properties (like which columns are shown) in a target form?
I thought setting its modifier to public will suffice.
That should do it, then you can address the grid through your user control instance. Assuming you control is named "MyControl" and your grid within the control is named "MyGrid" then you should be able to use MyControl.MyGrid. to get to the properties.
You can add properties to your UserControl that helps you to change design of your Control from different forms.
Problem not solved in that general way I initially posed it.
As a quick hack I declared public properties for some of the grid properties I needed (like Columns collection)
Tnanx for your help, though.

Categories

Resources