I'm looking to pass data to a WPF window from a WinForm and receive a message back from the WPF window.
My code is a mix of random online tutorials and HighCore's log viewer. I have a WinForm that launches my new WPF window in the following fashion:
private void openTransactionViewToolStripMenuItem_Click(object sender, EventArgs e)
{
var transactionViewWindow = new TransactionViewer.MainWindow();
ElementHost.EnableModelessKeyboardInterop(transactionViewWindow);
transactionViewWindow.Show();
transactionViewWindow.Test = "test"; // testing out data passing
transactionViewWindow.AddTest();
}
My MainWindow.xaml.cs looks like:
public partial class MainWindow : Window
{
public ObservableCollection<Session> SessionList { get; set; }
public string Test{ get; set; }
public MainWindow()
{
InitializeComponent();
SessionList = new ObservableCollection<Session>();
SessionList.Add(new Session() { BeginLine = 0, EndLine = 1, Message = "some message" });
SessionList.Add(new Session() { BeginLine = 2, EndLine = 3, Message = "another message" });
SessionItems.ItemsSource = SessionList; // the ItemsControl
}
public void AddTest()
{
SessionList.Add(new Session() { BeginLine = 4, EndLine = 5, Message = Test });
}
}
public class Session : PropertyChangedBase
{
public int BeginLine { get; set; }
public int EndLine { get; set; }
public string Message { get; set; }
}
where PropertyChangedBase inherits from INotifyPropertyChanged. I have an ItemsControl bound to Message. My output looks like:
some message
another message
test
"Data passing" is successful! Eventually, when the WPF window loads I want to pass a List<Session> from my WinForm that will be used to populate the ItemsControl. I also want to have a button on the WinForm that will send a List to repopulate/refresh the data in the WPF. From the current behaviour I think this will be possible even with my current, simple implementation (just updating SessionList).
Is there a more appropriate way of doing this? Events, for example? Do I need to fire off an event in order to tell my WinForm that the WPF has successfully added all Session objects, or whenever a user clicks on a specific one?
Any benefit to using MVVM here?
I've been developing for WinForms for a while and finding the transition to WPF quite confusing. Hopefully someone can help out with some guidance or code examples.
Edit: for future reference, a decent MVVM tutorial targeted to people like me can be found here: http://jpreecedev.com/2013/06/08/wpf-mvvm-for-winforms-devs-part-1-of-5/
You approach seems OK to me. It's not perfect, but it is workable enough.
An optimal approach, IMO, would be to create a ViewModel for the WPF Window, instead of directly referencing the Window itself when passing data back and forth.
The idea is:
public class MyForm: Form
{
public TransactionViewerViewModel TransactionViewer {get;set;}
//... other code...
//Form constructor:
public MyForm()
{
InitializeComponent();
//Create ViewModel:
TransactionViewer = new TransactionViewerViewModel();
}
private void openTransactionViewToolStripMenuItem_Click(object sender, EventArgs e)
{
//Create WPF View:
var transactionViewWindow = new TransactionViewer.MainWindow();
//Interop code
ElementHost.EnableModelessKeyboardInterop(transactionViewWindow);
//Set DataContext:
transactionViewWindow.DataContext = TransactionViewer;
//Show Window:
transactionViewWindow.Show();
//Call methods on the ViewModel, rather than the View:
TransactionViewer.Test = "test"; // testing out data passing
TransactionViewer.AddTest();
}
}
So, the ViewModel would be something like:
public class TransactionViewerViewModel : PropertyChangedBase
{
public ObservableCollection<Session> SessionList { get; set; }
public string Test{ get; set; }
public TransactionViewerViewModel()
{
SessionList = new ObservableCollection<Session>();
SessionList.Add(new Session() { BeginLine = 0, EndLine = 1, Message = "some message" });
SessionList.Add(new Session() { BeginLine = 2, EndLine = 3, Message = "another message" });
}
public void AddTest()
{
SessionList.Add(new Session() { BeginLine = 4, EndLine = 5, Message = Test });
}
}
This achieves a perfect separation between the WPF UI and the actual data / business logic, to the point that you can even create Unit Tests for your ViewModel.
And, since you're setting the ViewModel as the Window's DataContext, you will need to access all your ViewModel properties via DataBinding, rather than procedural code:
<ItemsControl ItemsSource="{Binding SessionList}"/>
Then, you may want to introduce delegates or events in the ViewModel, and listen to these in your Form, thus achieving WPF => winforms communication.
Related
I created WPF on the MVVM principle, but I can't make it possible to open another one from the current page.
I followed this example
We need something like this:
Example
It is also worth considering that a page instance should be created. (That is, so that you can open yourself from Page 2, but with a different name)
My failed attempt:
Page 2 ViewModel
public Page2ViewModel()
{
ButtonCommand = new RelayCommand(o => LoadOtherView());
}
public string Title { get; set; } = "Page2";
public string Text { get; set; } = "Page two";
public ICommand ButtonCommand { get; set; }
private void MainButtonClick(object sender)
{
//MainViewModel main = new MainViewModel();
//main.SelectedPageViewModel = main.PageViewModels[0];
//main.SelectedPageViewModel.Title = "да";
//main.SelectedPageViewModel.Text = "Первая страница";
}
private void LoadOtherView()
{
// Instead of interacting with a whole ViewModel, we just use the interface
//_pageDisplay.ChangePageCommand.Execute(new ContactViewModel());
_pageDisplay.ChangeViewModel(_listPageViewModels[0]);
}
Interfaces
namespace WpfMVVMCore.Interfaces
{
public interface IPageDisplay
{
public IPageViewModel GetCurrentPage();
public IList<IPageViewModel> ListPageViewModels();
public void ChangeViewModel(IPageViewModel newPage);
}
}
MainViewModel
public MainViewModel(IPageDisplay pageDisplay, IList<IPageViewModel> ListPageViewModels)
{
_pageDisplay = pageDisplay;
_pageViewModels = ListPageViewModels;
}
public IPageViewModel GetCurrentPage()
{
return _selectedPageViewModel;
}
public void ChangeViewModel(IPageViewModel newPage)
{
this.SelectedPageViewModel = newPage;
}
public IList<IPageViewModel> ListPageViewModels()
{
return _pageViewModels;
}
** If the information provided by me is not enough for you, you can download this project (with my unsuccessful attempt 🤭 ):download**
P.S. Please do not criticize me if it is not difficult for you. I'm new to MVVM. Better help :)
I'm new to MVVM and am converting a WinForms project to WPF using the MVVM Light framework. Most introductions to MVVM emphasize that a business model should have no knowledge of a view model. So I'm modifying my business models to support my new view models through the addition of public properties and property-changed events.
But this feels awkward when I just want to get user input that I'm not going to save in the model. In WinForms, I would do it this way in my business model:
dlg.ShowDialog();
string someValue = dlg.SomeValue;
// Use someValue in a calculation...
Is this really anathema to MVVM:
window.ShowDialog();
string someValue = _ViewModelLocator.MyVm.SomeValue;
It saves me from having to create a public property in the business model for what only really needs to be a local variable.
Thanks for advice & insights.
Here's a post I wrote on unit testing a user-interaction (i.e. dialogs).
I recommend using an interface to wrap around your user interaction logic.
Leveraging a user interface with delegates will provide an object oriented solution.
The thought process is to unit test your user interaction without user intervention.
In addition, I added this implementation for discovery on Nuget.
I believe the class name on that dll that you want to use is called UserInteraction.
public delegate MessageBoxResult RequestConfirmationHandler(object sender, ConfirmationInteractionEventArgs e);
public interface IConfirmationInteraction
{
event RequestConfirmationHandler RequestConfirmation;
MessageBoxResult Confirm();
}
public class ConfirmationInteraction : IConfirmationInteraction
{
#region Events
public event RequestConfirmationHandler RequestConfirmation;
#endregion
#region Members
object _sender = null;
ConfirmationInteractionEventArgs _e = null;
#endregion
public ConfirmationInteraction(object sender, ConfirmationInteractionEventArgs e)
{
_sender = sender;
_e = e;
}
public MessageBoxResult Confirm()
{
return RequestConfirmation(_sender, _e);
}
public MessageBoxResult Confirm(string message, string caption)
{
_e.Message = message;
_e.Caption = caption;
return RequestConfirmation(_sender, _e);
}
}
}
public class ConfirmationInteractionEventArgs : EventArgs
{
public ConfirmationInteractionEventArgs() { }
public ConfirmationInteractionEventArgs(string message, string caption, object parameter = null)
{
Message = message;
Caption = caption;
Parameter = parameter;
}
public string Message { get; set; }
public string Caption { get; set; }
public object Parameter { get; set; }
}
In my XAML code I've got a combo box that is bound to a static property as shown below.
<ComboBox x:Name="DifferentKinds"
ItemsSource="{x:Static local:MainWindow.DifferentKinds}"/>
And the code for the property and its source.
public static Kind[] DifferentKinds
=> (Kind[])Enum.GetValues(typeof(Kind));
public enum Kind { WeeHee, BuuHuu }
I just learned that there'll be more kinds in the future. They won't be created particularly often but it's uncertain how many they might become with time. So, instead of adding new elements to the enum, I'll read in these from the DB.
For the simplicity of the example, let's say we read in those values every time the property is accessed. The solution becomes a private fields that is read in from the DB before the execution of InitializeComponent() starts. Then, I serve those values as a static property still, like so.
public MainWindow()
{
PopulateDifferentKinds();
InitializeComponent();
}
private static IEnumerable<Kind> _allDifferentKinds;
public static IEnumerable<Kind> AllDifferentKinds
=> _allDifferentKinds.Where(element => element.Active);
public class Kind
{
public String Name { get; set; }
public bool Active { get; set; }
public override string ToString() { return Name; }
}
Is this approach creating a huge problem that I miss to see?
Is there a better way to bind the items in the bombo box to the values from DB?
The main problem I see here is that calling the PopulateDifferentKinds method in the view's constructor will create a performance problem. While this method is running and the database is being queried, your UI is being blocked.
This could be improved using a class that loads your data on a background thread and uses a PropertyChanged event to signal that the data has been loaded:
public class Kind
{
public string Name { get; set; }
public bool Active { get; set; }
public int Value { get; set; }
}
public class AppEnumValues : INotifyPropertyChanged
{
private static readonly Lazy<AppEnumValues> current
= new Lazy<AppEnumValues>(() => new AppEnumValues(), LazyThreadSafetyMode.ExecutionAndPublication);
public static AppEnumValues Current
{
get { return current.Value; }
}
public Kind[] AllDifferentKinds { get; private set; }
public bool IsLoaded { get; private set; }
private AppEnumValues()
{
Task.Run(() => this.LoadEnumValuesFromDb())
.ContinueWith(t => this.OnAllPropertiesChanged());
}
protected virtual void OnAllPropertiesChanged()
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(string.Empty));
}
}
private void LoadEnumValuesFromDb()
{
// This simulates some latency
Thread.Sleep(2000);
// Call your data service here and load the values
var kinds = new[]
{
new Kind {Active = true, Name = "WeeHee", Value = 1},
new Kind {Active = true, Name = "BuuHuu", Value = 2}
};
this.AllDifferentKinds = kinds;
this.IsLoaded = true;
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
You could extend this with properties for each extensible "enum" you need in your application. Implementing the Singleton pattern, this would load its data in background the first time it is used. You could bind your ComboBoxes like this:
<ComboBox ItemsSource="{Binding Source={x:Static wpfApplication2:AppEnumValues.Current},Path=AllDifferentKinds}"
IsEnabled="{Binding Source={x:Static wpfApplication2:AppEnumValues.Current},Path=IsLoaded}"
DisplayMemberPath="Name" />
While the data is being loaded, the ComboBox would be disabled.
I would recommend looking into MVVM and Dependency Injection. This will enhance your WPF application architecture and make things like that easy: You wouldn't provide a static property or singleton, which has bad testability and extensibility, but you could use constructor injection to give the AppEnumValues provider into your View Model and then bind your view to it.
I'd like a NodeView to display some hierarchical data, something like this:
Father Mother
====== ======
Jon Ann
+Sons
+--Jon 20
+--Dave 10
+Daughters
+--Ann
Ron Mary
Paul Eve
+Sons
+--Bob 4
"Sons" and "Daughters" should not be shown if they are empty.
I have created three bussiness classes: Parents, Son and Daughter, and I'm creating TreeNode subclasses to display them.
I designed a Window with Monodevelop designer, and recreated the nodeView with code. The nodeview doesn't display anything at all, and I would like to know why. Here's the code, in a single file so anyone can test it:
using System;
using Gtk;
using System.Collections.Generic;
using System.Linq;
namespace Family.Model
{
public class Son {
public string Name {get;set;}
public string Age {get;set;}
public Son(string n,string a) {
Name=n;Age=a;
}
}
public class Parents
{
public string Father {get;set;}
public string Mother {get;set;}
public List<string> Daughters {get;set;}
public Dictionary<string,Son> Sons {get;set;}
public Parents() {
Daughters=new List<string>();
Sons=new Dictionary<string, Son>();
}
}
}
namespace Family.View
{
using Family.Model;
[TreeNode (ListOnly=false)]
public class ParentsNode:TreeNode
{
private Parents parents;
public ParentsNode (Parents p):base()
{
this.parents=p;
DaughtersRoot rootd=new DaughtersRoot();
SonsRoot roots=new SonsRoot();
if (p.Sons.Count>0) {
this.AddChild (roots);
p.Sons.Values.ToList ().ForEach (x=>roots.AddChild(new SonNode(x)));
}
if (p.Daughters.Count>0) {
this.AddChild (rootd);
p.Daughters.ForEach (x=>rootd.AddChild(new DaughterNode(x)));
}
OnChanged ();
}
[Gtk.TreeNodeValue(Column=0) ]
public string Father {
get { return parents.Father;}
set { parents.Father=value;OnChanged ();}
}
[Gtk.TreeNodeValue (Column=1)]
public string Mother {
get { return parents.Mother; }
set { parents.Mother=value;OnChanged ();}
}
}
[TreeNode(ListOnly=false)]
public class DaughtersRoot:TreeNode
{
[Gtk.TreeNodeValue(Column=0) ]
public string Label {
get {return "Daughters"; }
}
}
[TreeNode(ListOnly=false)]
public class SonsRoot:TreeNode
{
[Gtk.TreeNodeValue(Column=0) ]
public string Label {
get {return "Sons"; }
}
}
[TreeNode(ListOnly=false)]
public class DaughterNode:TreeNode {
private string mName;
public DaughterNode(string s):base() {
this.Name=s;
}
[Gtk.TreeNodeValue(Column=0) ]
public string Name {
get {return mName;}
set {mName=value;OnChanged ();}
}
}
[TreeNode(ListOnly=false)]
public class SonNode:TreeNode {
private Son son;
public SonNode(Son s):base() {
this.son=s;
OnChanged ();
}
[Gtk.TreeNodeValue(Column=0)]
public string Name {
get { return this.son.Name; }
set {son.Name=value;OnChanged ();}
}
[Gtk.TreeNodeValue(Column=1)]
public string Age {
get { return this.son.Age; }
set {son.Age=value;OnChanged ();}
}
}
public class MainWindow: Gtk.Window
{
private global::Gtk.ScrolledWindow GtkScrolledWindow;
private global::Gtk.NodeView treeFamily;
private NodeStore storeParents=new NodeStore(typeof(ParentsNode));
protected virtual void Build ()
{
global::Stetic.Gui.Initialize (this);
// Widget MainWindow
this.Name = "MainWindow";
this.Title = global::Mono.Unix.Catalog.GetString ("MainWindow");
this.WindowPosition = ((global::Gtk.WindowPosition)(4));
this.GtkScrolledWindow = new global::Gtk.ScrolledWindow ();
this.GtkScrolledWindow.Name = "GtkScrolledWindow";
this.GtkScrolledWindow.ShadowType = ((global::Gtk.ShadowType)(1));
this.treeFamily = new global::Gtk.NodeView ();
this.treeFamily.CanFocus = true;
this.treeFamily.Name = "treeFamily";
this.GtkScrolledWindow.Add (this.treeFamily);
this.Add (this.GtkScrolledWindow);
if ((this.Child != null)) {
this.Child.ShowAll ();
}
this.DefaultWidth = 400;
this.DefaultHeight = 300;
this.Show ();
this.DeleteEvent += new global::Gtk.DeleteEventHandler (this.OnDeleteEvent);
}
public MainWindow (): base (Gtk.WindowType.Toplevel) {
Build ();
Parents p=new Parents();
p.Father="Bob";
p.Mother="Mary";
storeParents.AddNode (new ParentsNode (p));
p=new Parents();
p.Father="Ron";
p.Mother="Ann";
p.Sons.Add ("David",new Son("David","20"));
p.Sons.Add ("Matt",new Son("Matt","10"));
p.Daughters.Add ("Elaine");
p.Daughters.Add ("Kate");
storeParents.AddNode (new ParentsNode(p));
this.treeFamily=new NodeView(storeParents);
Gtk.TreeViewColumn fatherColumn = new Gtk.TreeViewColumn
("Father",new CellRendererText(),"text",0);
Gtk.TreeViewColumn motherColumn = new Gtk.TreeViewColumn
("Mother",new CellRendererText(),"text",1);
treeFamily.AppendColumn (fatherColumn);
treeFamily.AppendColumn (motherColumn);
treeFamily.ShowAll ();
}
protected void OnDeleteEvent (object sender, DeleteEventArgs a) {
Application.Quit ();
a.RetVal = true;
}
}
class MainClass
{
public static void Main (string[] args) {
Application.Init ();
MainWindow win = new MainWindow ();
win.Show ();
Application.Run ();
}
}
}
Gtk model binding works quite different than other toolkits. That being said, here is what you need to know for starters:
There are several types of "controls" or widgets as they are called in GTK that can display matrix/list data:
NodeView (easiest)
TreeView
Now, AFAIC, NodeView only exists in GTK#, that is, the .Net bindings. TreeView is available in all other bindings and is part of the GTK+ Core. The difference is that somehow, NodeView is easier to use but more limited.
You'll want to use NodeView if you only need to display list data, that is, no hierarchical data (as you seem to need though)
If you need to display hierarchical data then you'll use a TreeView.
Whichever you use, you'll also have to set up what you need in that widget to display, your columns for example. The difference between a NodeView/TreeView cells in GTK and other basic toolkits is that a NodeView column for example can display other widgets inside its cell instead of just text, so you could have one column that has a cell that displays a progress bar, or a checkbox. The widgets that you use inside these cells are called CellRenderers and there are CellRendererText, CellRenderToggle, etc.
Now, these controls are "bound" to data by "stores", for example:
NodeStore
ListStore
TreeStore
Which one you'll use depends on your needs again and the type of widget you are going to use, so if you only need to display simple data use NodeStore, if you need to display hierarchical data use a TreeStore. One powerful thing about these data stores is that you can store in them not only the data that is shown on the NodeView/TreeView widget but any other data that does not necessarily needs to be displayed, you can even store objects, for example, you could have a store with 4 columns of which 3 are shown on the widget and the fourth keeps an instance of the full object.
In this link theres samples for each case I mentioned that you can try. I think that you should be using the "Tree" model to accomplish what you want and not the "Node".
GTK is a powerful toolkit but sometimes its hard to understand how it works. I hope this intro works for you.
I have a WPF Windows app. The viewmodel calls a method in the format of Model.TrySomething(), which returns a boolean if anything in TrySomething logically fails. If false is returned, the UI can throw a message back to the user.
What is the best way to bubble this message up from the model?
This is how we do it on our projects. Works fine:
// your event args might include more properties
public class ShowMessageBoxEventArgs : System.EventArgs
{
public string Title { get; set; }
public string Text { get; set; }
}
// example of your model base
public class MyModelBase
{
public event EventHandler<ShowMessageBoxEventArgs> ShowMessageBox;
protected void RaiseShowMessageBox(string title, string text)
{
if (ShowMessageBox == null)
return;
var _Args = new ShowMessageBoxEventArgs
{
Text = text,
Title = title
};
ShowMessageBox(this, _Args);
}
}
// for this sample, this is your view model
public class MyModel : MyModelBase
{
public void DoSomething()
{
// TODO: Do Something
base.RaiseShowMessageBox("DoSomething", "Complete!");
}
}
// this is your window or in app.xaml.cs (where we do it)
public partial class MainWindow : Window
{
MyModel m_MyModel = new MyModel();
public MainWindow()
{
InitializeComponent();
this.DataContext = m_MyModel;
Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
bool m_Loaded = false; // only once
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
if (m_Loaded)
return;
m_Loaded = true;
// allow model to show messagebox
m_MyModel.ShowMessageBox += (s, arg) =>
{
MessageBox.Show(arg.Text, arg.Title);
};
}
}
Best of luck!
If the message which you want to display is a modal dialog, you can write a service (lets name it MessageDialogService) which is injected in your viewmodel and then call a MessageDialogService.Show() method. This method creates a new WPF window and shows the message.
This service can then be used in any of your ViewModels to show messages.