Move data between windows? - wpf

I have been trying to move data from one WPF window to another while both are open without making a new instance.
This feels like it should be really easy but after hours of researching I cant get anything to work.
Here is what I have tried so far. This works, however, it creates another mainform.
Dim mainform As New MainWindow
mainform.TextBox3.Text = TextBox1.Text
mainform.Show()
if I try this without the word new it gives me an error.
I am totally puzzled by this.

Use MVVM pattern. Your ViewModel will contain all data that you need to be copied to the other window. Your Window class will have a constructor that accepts a ViewModel. Now just write a copy constructor for your ViewModel, create a new Window with that copy, and you're done.

I suggest you take a look at the MVVM concept, which will give you a great foundation for understanding how to setup your WPF application. It appears that you lack the basic understanding of WPF components necessary to achieve proper results. While there are many ways of developing WPF application, MVVM will provide that necessary base. Moreover, I suggest looking at MSDN and other websites that would provide beginner tutorials. By reading your question, I feel that you're trying to do something without really know what you're doing. I don't suggest that it's a bad thing in any way - I believe that the best way to learn is by doing, but you need to acquire some guidance in a manner of tutorials or perhaps a book.

Related

ReactiveUi how to use ICollectionView with the Getting started example

I'm learning ReactiveUi and trying the sample from here https://reactiveui.net/docs/getting-started/ This is a great examle, but now I would like to replace the List SearchResults with a ICollectionView to have more grouping / sorting capability.
The documentation contains a ReactiveCollectionViewSource here and but I'm unable to find a sample how to use it with the starting code. Is that the right track?
The ReactiveCollectionViewSource is for the iOS project only.
You should be able to use standard WPF ways of generating your CollectionView and use CollectionViewSource.GetDefault() etc.
You can then use WPF or ReactiveUI bindings to bind that to your controls.
One thing of note is that DynamicData is going to be in the near future the preferred solution for data management in RxUI projects. Worth seeing if it fits in your problem space. https://github.com/RolandPheasant/DynamicData - There is a DynamicData channel on the Reactive slack channel if you need help with the framework https://reactiveui.net/slack

Printing reports from WPF

I've been searching the web for over 2 days now for a good and easy to use something to print reports from a WPF application.
I've been trying to use the DocumentViewer but it's a pain to add Controls to it and style them. Is it me or can this only be done in code behind and how can a grid over multiple pages dynamically be generated?
Second I found the control FlowDocumentReader. This looks like an interesting thing since you can add Tables which are much easier to style and I was able to do this in xaml. Unfortunately, I have no clue how to print from it. I tried to edit the template of the reader but without success.
Last I found this post (What's the best approach to printing/reporting from WPF?) but it's dated to 2008 so I'm not sure if the information is still accurate.
My conclusion is that it is pretty hard to get a nice looking report printed from WPF without writing lots of code behind or editing templates which (by me) fail to do what I want.
So my question is, is there any good approach to my problem? I'm really bothered that I lost this much time for something that I thought was so trivial to do.
I was wonder, what about this guy's approach: https://stackoverflow.com/questions/17647939/how-do-we-handle-different-printing-needs-by-using-fixeddocument?rq=1?
Crystal Reports is indeed a good choice for complicated reports but if your reports will be simple in design I'll recommend using CodeReason reports, it's an xaml based reports. Too bad it's not updated anymore but it's really easy to use for simple reports.
For more information: http://wpfreports.codeplex.com/

dymamic text binding in a WPF TextBlock

Sorry for the noob question.... but
I have a rather large unilingual application that I have to make multilingual, meaning no resource file. I don't have the option of using the culture information but need to do it more on the fly at runtime so that the user can change languages either on startup or menu pick while in the application. I can handle that part ok. I realise there are traditional ways to set these values but I'm hoping to find a better solution.
What I'd like to be able to do is the following
First of all is this even possible? I've taken a pretty good look around and didn't really find anything close to what I'd like to do. I would even be willing create a few user controls if that was the solution. In the end I'll have to do this for buttons, labels, datagrid headers and messages(these are easy lol)
Any thoughts would be appreciated.
Thanks
Yes it is possible. Try this article: Creating an Internationalized Wizard in WPF.
Specifically look at the startup code for the application where CurrentCulture is modified for the CurrentThread.
Do not feel locked out of using per-culture resources by WPF. Different resources can be used via the "PublicResXFileCodeGenerator" as described in the article.
The MSDN Docs - CultureInfo.CurrentCulture Property may help.

How can I write WPF efficiently?

When I first learned about Microsoft's then-new framework for developing desktop applications, WPF, I thought that one of the biggest benefits of it would be its efficiency. After all, I already know what the GUI controls and elements were that I wanted to use--I just have to slap them all on the page and then wire up all my bindings right, and I'll be done. After that, I learned about the MVVM pattern, which promised clean separation of concern within my WPF app.
I thought this was going to be great! I got into creating several different admin and data entry WPF apps with my team at work, and thus I began to crank out working software with robust but simple GUIs, right?
Not so fast, there, cowboy coder. My experience is that writing WPF is S-L-O-W.
Well, at least the way I do it. You see, after I have a picture of my GUI on a whiteboard, I code up the XAML with the controls that I want. This part, as expected, is fast--laying out the whole of a window is pretty quick. After that, its all the little stuff you want these elements to do takes awhile. I always seem to want to make this text bold in some cases; show a red error box in these other cases.
Then things unravel: this binding isn't working right over here--I have to write a converter and adjust the layout for the right values. Whoops, I forgot that extra NotifyPropertyChanged there. Oh, and I want to use a different template in this state vs. that, so I have to figure out what I can use to swap the templates in certain situation. This data is coming in asynchronously, so I need to make sure the right thing is happening on the right thread and that Property gets NotifyChanged as well. Crap, when I maximize my window, it doesn't stretch like I thought it would--must be because its container height isn't defined, but I don't want to define that, so I have to make it stretch in its parent. Oh, now I really want to make a user control out of this stuff over here, so I better implement some dependency properties...
On and on I go, spending hours and days on stuff that just feels so small and minor. I soon resort to cutting usability features and look-and-feel enhancements because this is taking just too darn long.
Does anyone have any tips or principles I need to try in order to write WPF efficiently?
A couple of things that have saved a lot of time for me:
Use DockPanel as your default panel for layout unless you have a good reason not to.
Keep a folder full of useful classes: a ViewModelBase class that implements INotifyPropertyChanged, a RelayCommand class, etc. There's no need to get fancy and try to make this a separate assembly that you build into your project; just write reasonably good implementations and copy/paste them into your new projects.
Get Resharper and use it. Use templates for creating dependency properties, properties that do change notification, and commands.
Find or build a good library for asynchronous task management.
I find that even for very simple applications I get more done faster with WPF than I did with Windows Forms. For applications that aren't very simple, there's absolutely no comparison.
For the most part, WPF applications are a lot of work to develop because it's harder to make the case for cutting out UI features. You can't just say, "Oh, that's not possible," because it probably is possible (whatever "it" is).
Write your own library, or find an existing one.
WPF is great, but out of the box it is missing some things that would make coding faster. I got tired of writing the same things repeatedly, so I ended up creating my own library full of things like converters, visual tree helpers, attached properties, custom controls, etc., and since then, my development time has sped up considerably.
In addition to my own library, I've also started using Microsoft's Prism and Galasoft's MVVM Light Toolkit. Both contain useful objects that I use all the time and are better than what I could code on my own. (Most used are NotificationObject from Prism, RelayCommand from MVVM Light Toolkit, and EventAggregator or Messenger from either one depending on the project, etc.)
Another thing I've done to speed up coding time is to take advantage of Visual Studio's macros. For example, to create a property with Change notification, I write the private property, hit Ctrl+E, Ctrl+R which generates the public version, then run a macro which automatically sets up the PropertyChanged notification in the setter method.
I almost never change the setter methods from the default macro'd one, and instead use the PropertyChanged event to handle any changes that should occur on the setter. This not only makes it easier to track application flow, but also greatly reduces the time I used to waste browsing through my public properties to alter a setter method.
I believe the right answer isn't for WPF at all, but it can fit what you're looking for.
Most of the times, when you want to leverage a new technology there's a time while you're not efficient, productive and your solutions aren't that impressive, innovative or just doesn't look like others.
What will give you more efficiency is working with WPF itself.
It's more about project management topics than programming. After finishing some project, your team and you should go to some room and discuss:
Success stories.
Problems during development.
Pros and cons.
Fails in the application architecture.
Communication problems within the team and customer.
... and so on.
If everyone shares their knowledge, project manager or team leader does a good job documenting each project story, finally everyone will have a "know-how".
In addition, it's important that you won't need to reinvent the wheel for every new project: if some pattern worked fine, do the same way next time, even if it's not the best way of doing it. And try to enhance it, if possible.
Design patterns, technologies, paradigms, languages, companies, colleagues and nothing are a silver bullet: Microsoft said WPF is a step-forward in Windows client developments, and it is that: a more modern approach to provide shinny user interfaces and a programming paradigm that fits nowadays' desired approaches, easing the relation between coders and designers, as WPF has XAML, which allows not only separation of concerns, but separation of professionals by area (designers, UI programmers, business programmers, ...).
Finally, as I said above, WPF won't be your silver bullet: learn from your own success and read a lot, see sample applications, download open source solutions, listen your colleagues, drink a coffee and, after all, after some headaches, some day in the near future, you'll leverage these technologies (and many others).
EDIT
I'd like to add that a good way of using the know-how is creating a Visual Studio guidance pack, so you can automate a lot of tasks like creating managers, views, models and other things just in the way your team would do by hand.
For example, you can create a guidance pack for a WPF CRM-like application and you can automate module creation. When you want to add a new module, guidance pack starts a process which adds all the necessary classes to start development this new module, and it can create a sample form already associated with a navigation manager, controller or whatever (it's just an example).
Guidance pack and T4 would be both good tools for automating tedious or repetitive tasks in everyday's tasks:
http://msdn.microsoft.com/en-us/library/ff631854.aspx
http://msdn.microsoft.com/en-us/library/bb126445.aspx
I have been using WPF since 2008 and can honestly say to do it right and clean does take more time than the same thing in WinForms would take to develop. I have written a lot more WPF than Winforms. That being said - if I need a simple internal utility - it is ALWAYS Winforms. If I have something forward facing to a client - it is always WPF. Why? Winforms are cheap and dirty and you get a lot for free. What you don't get is the fit and polish that WPF can provide. The flexibility with WPF does come at a cost - but in the longer run it's worth it for public facing software.
Yes WPF is a hurdle but it also has rewards. You are on the right track with a design pattern such as MVVM. Sounds like you have not even gotten to the "rewards" of dependency properties or event bubbling. But the control over the UI is great. Almost everything is a content control. In forms I was always writing custom controls to get the UI I wanted. In WFP I have never had to write a custom control for UI and doubt I ever will. The syntax is new but it is compact - I rewrote a Form app in WPF and the WPF has 1/3 the lines and more features. Read a whole book on WPF just to get grounded - I like PRO WPF in C# 2010. You could also say LINQ is complex but man does it do a lot in just a few key strokes. WPF is not something you just pick up on the fly as you next application.

Best ways to manage all the Data taken from Tables in a Database and stored in a WPF App

Hi i am trying to find the best way to manage the Data (that comes from tables in a Database) in your App.
My main question is, what is the best approach to take to then i successfully work with Bindings, Notifying Properties/Collections Changes?
My App is to manage the Inventory of all kind of things in an Enterprise, so i collect the Data to my App, and then i am using DataViews to store it? But this is the best approch to then i can work with bindings and Notifying Changes?
I am recently entering in WPF (about 2 months), the concept of ViewModel it's what i am trying to understand now, because seems that is the most thing used to manage the Data stored in an WPF App. Or maybe i am understanding all wrong.
What I am asking is for advices and examples to then i understand which is the best aprroach.
Note: In my case i have a Main Window with a DataGrid which have DataGridTextColumns and DataGridComboBoxColumns, and i have auxiliary Windows to manage Categories, Places and etc with ListViews.
Thanks in advance!
When we are talking about WPF, then of-course we are talking about MVVM pattern, its the best way :)
This link is the best start point for MVVM pattern:
MVVM for Beginners

Resources