Access data source of main program in user control - c#

I would like to write a program that uses several tabs, each showing a data grid. The data of all the grids is stored in a single data source. Each tab should look identical except for the number of colums/rows and the values of course.
So I am now trying to put the grid view into a user control and create a .dll. If a tab is added I put the user control into it - this works already. The grid works in virtual mode and if a new tab/grid is created it calls the event to load the data into the grid.
Now I have the problem that the grid and the LoadData event are in the user control while the data source is in my main program. So in the user control the data source is undefined. I thought that this would be ok because you do not run the user control alone and once it is used in the main program the data source should be available to the LoadData event. However, it does not seems to be possible to generate the .dll while the data source is unknown.
On the other hand I do not want to put the data source into the user control because then it is dublicated every time a new tab is created.
I hope you can give me a hint what is the best way to implement it.
thank you very much!

I don't know what answer (how detailed) you expect, but I think this is a very good example to:
reuse one control
use MVVM architecture pattern
I would try to seperate class which would execute logic responsible for creating ViewModel. Control (grid in your purpose) would be always binded to this ViewModel, and this Control would be reuse in different Views. Depend on where it would be placed you can add some additional features if it is necessary (Decorator design pattern). If you expected more detailed (not conceptual) solution than sorry.

Related

Change WPF template/theme/UI/control at runtime

I'm very new to WPF. I have to create an WPF app which will do the following:
1) User can change the UI at run time. Controls in each UI are fixed. Suppose two textboxes for numeric value accept and command button to perform multiplication operation and alert the result.
2) Most importantly user can browse the folder of XAML files (UI) and select them / change then at run time. There can be 'n' number of UI users can create and use.
3) I want to use MVVM pattern.
I found this as best example but with doubt. Doubt is that, can user browse user controls and select then, set them? Do they need to be created and added to list before build see this ?
Which is the best way to achieve this task?
If you follow the MVVM pattern, you should create a View (implemented by an User Control) for each UI that your user selects. The Main View will have a ContentControl (or another control type that can host user controls like a HeaderedContentControl) which can host the other Views that the user selects. The view changes will be performed by the associated view model, just like the example you gave.
Trying to answer your question, you should create a menu where the user can select the View to be displayed. The existing views don't need to be added to a list before, because they will already exist in your project. If you want a more detailed example, with code, just update your question with some example you want, like the user can select two views from a menu for instance, and I will add some code .

c# binding controls work in form, but not user control

I have a mysql database that I am using my Windows form application to view and fill data in.
I'm running into a problem when I try to use binding sources in a user control. All I'm doing is clicking and dragging a label from the data sources toolbar.
When I run the program, the label can't reference the fields in my database. The correct data sources are displayed at the bottom of the design view and in the Designer code.
But, when I do the same thing in the form, it works perfectly well.
What changes between the form data binding and the user control data binding? I've tried digging in the Designer code, creating an entirely new project so I don't have any messy code in the way.
Edit: The article,
Walkthrough: Creating a Windows Form user control that supports simple data binding
makes it look like I can simply drag an drop on the user control, no question asked. However, this does not work for me

How do I keep the MVVM pattern while implementing lookup dialogs?

I am in the process of recreating a Win32 (Delphi 5) screen using WPF in C# with the MVVM pattern. The purpose is to create a demo that I can use to push my company to make the move from Delphi 5 to C#. In this demo I want to do things the "right" way so we have a good example to start with. With that in mind, I am trying to adhere to MVVM as strictly as possible which is where I am currently running in to a problem.
In the original program/screen we have lookup dialogs that we can attach to edit boxes and grid cells. The way it works is that the lookup dialog is an implementation of a specific search (i.e. Customer lookup). When you attach the lookup dialog to a control, it listens to the key press events for the control. It has a progressive search feature so that when you start typing, if it can find results that start with what you are typing it will insert the first result in control you attached it to. While doing this it keeps the cursor where you were typing and highlights the part of the data that you typed in so that if you keep typing it can narrow down the results to find the more specific answer you were looking for. Where the dialog part comes in to play is when you hit the ellipse button next to the control or if you type in something that it couldn't find while doing the progressive search. In either of those cases it will bring up the lookup gui which will show you all the possible results, provide filters to help you narrow your search down etc. This gui is visually attached to the control you assigned it to. It always pops up right under the cell or edit box etc that you assigned it to.
So this lookup dialog is throwing me off when it comes to trying to figure out how to implement it using MVVM. So far my code mostly pure as far as MVVM goes. The only code in the Views code behind is the one line creating the ViewModel and assigning it to its DataContext. The ViewModel makes no references to the view. But with this lookup dialog the lines seem a little blurred to me. I envision the lookup having it's own ViewModel. When you lookup something, say an Item to be put in a Sales Order, we may want to get information like the Item's price from the lookup so that it can be copied in to the Sales Order record. So to do that the main ViewModel for the screen needs to know about the lookup's ViewModel. The View for the main screen needs to know about the View for the lookup too though because it has to be able to "attach" the lookup to a gui control so it can listen to key press events and know where to visually popup when it needs to be shown. I'm not sure if the code determining the visual position of the lookup should reside in the lookups ViewModel which would break the MVVM concept totally or maybe in the lookup's view's code behind which would muddy the purity of the view a bit since now the View isn't just the XAML.
The original lookup, after being created, would be given a reference to the control that it should attach to. It would then attach functions within its object to the key press and button click events (our edit boxes can have buttons built-in). When it was told to popup, it would figure out the current position of the control it is attached to so that it could figure out where to put itself. In the case of attaching it to a grid, it was actually attached to the column of the grid because there is only one instance of the lookup, and it would figure out the current position of the cell in the grid it is supposed to be popping up under.
What is the best way to do this while still staying in the realm of MVVM. I really would like to stay as "pure" as possible so I can come back later and add integration and possibly unit tests for the ViewModels in the program and these should be ViewModels that are "headless" (no View).
To make it easier to understand what I am trying to achieve I have included a picture of the screen I am recreating for my demo with the lookup shown.

Is there a way to duplicate and access designer drawn controls in Winforms?

I want to create a set of controls in the designer and then replicate it over and over again. Here's a picture to help visualize what I'm describing:
Currently I program the position of each control to fit properly in the panel. Doing it this way is tedious and I have to run the program each time I make a change to make sure everything lines up the way I like it. This is the fourth iteration of this panel and I'm getting ready to do at least two more designs for different customers who want slightly different ways of displaying the same data.
Is there a way to draw the panel and its controls in the designer, the programmatically replicate the entire set of controls, and bind different data to each of them?
If you want to allow customer to move these controls around, you need:
Create persistence storage. Basically, your control needs to build itself from some sort of file or better - database, where all the positions and other properties will be set, saved, etc.
Develop a "design mode" for customer. Customer will click button "Design" and be able to move controls around, re-size them, etc. For you, in design mode you want to make controls druggable and also, you need to devise a way for resizing them too. Then customer clicks "Save" and control data should save into the persistence storage.
Now, if you ok with reimplementing multipple looks for customers and want to do it quickly - create a UserControl, place TableLayoutPanel on it and then use series of TableLayoutPanel's to give any look to your control. This is rather fast way. And if I only needed to do it 10 times, I would just create a control factory, and add every new implementation into it. Let customer pick out of existing implementations. Because creating designer will be rather involved effort.

Developing wizard UI - WPF

All in WPF:
Developing a wizard application, user has to answer a number of simple questions before brought to the main app. The main app is then prefilled with the information obtained from the wizard.
I started with a Window which I then planned to add usercontrols to. The main window would have the user control in the first row, then Next and Previous buttons to control moving between the controls in the second row. This way I could easily control the logic to switch between screens like:
WizardControl1.IsVisible = false;
WizardControl2.IsVisible = true;
But for some reason, user controls do not have setter for IsVisible. Hurray.
So then I thought I would just use seperate windows for each section of the wizard. The problem with this approach is that now when stepping between, the window opens in random positions, and by steppign through the wizard with next, the next window pops up randomly which is really distracting and frustrating.
So how can I develop a wizard properly? I don't get why this is so hard...not exactly rocket science... replacing text and controls and storing input after pressing next/previous!
Thanks
Check this link:
http://www.codeproject.com/KB/WPF/InternationalizedWizard.aspx
This is the article about building wizard in WPF by Josh Smith, it's seems to be nice pattern.
I found it's helpful for me, hope you'll too.
There is also an open source Avalon Wizard control on codeplex.
I'd probably aproach this using data binding and template selectors. Have the wizard form bind to a "WizardData" class, which exposes a list of "WizardPage" base classes.
The WizardData class can expose properties defining the correct info on the forms, and display a control for the main page that uses a template selector to determine the proper control to display based on the actual type of the particular wizard page.
It sounds like more work than it is, really. It also gives you the benefit of good separation between code and UI (all "work" is done by the WizardData and WizardPage classes), and the ability to test logic independent of the UI.
It's also a very WPF/MVVM way of approaching the problem.
I recognize this does not directly address your question, but I thought I'd mention it as a possible alternative. I've used Actipro's Wizard control with pretty good results, and when I have needed support, they have been very responsive. I am not affiliated with them in any way; I just like not having to write the plumbing to manage a wizard.
The property is called "Visibility".
I find that I do better when I dynamically add and removing controls rather than hide them.
I was looking for a Wizard solution too. I have the need to stick with stock WPF components so I implemented the wizard using a standard form and a tab control.
I only hide the tabs at runtime so there available in the IDE. At runtime just use Back, Next, Finish... to navigate thru the tab items
works good

Categories