Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing Data from WinForm to WPF
    primarykey
    data
    text
    <p>I'm looking to pass data to a WPF window from a WinForm and receive a message back from the WPF window. </p> <p>My code is a mix of random online tutorials and HighCore's <a href="https://stackoverflow.com/questions/16743804/implementing-a-log-viewer-with-wpf/16745054#16745054" title="log viewer">log viewer</a>. I have a WinForm that launches my new WPF window in the following fashion: </p> <pre><code>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(); } </code></pre> <p>My MainWindow.xaml.cs looks like:</p> <pre><code>public partial class MainWindow : Window { public ObservableCollection&lt;Session&gt; SessionList { get; set; } public string Test{ get; set; } public MainWindow() { InitializeComponent(); SessionList = new ObservableCollection&lt;Session&gt;(); 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; } } </code></pre> <p>where <code>PropertyChangedBase</code> inherits from <code>INotifyPropertyChanged</code>. I have an ItemsControl bound to <code>Message</code>. My output looks like:</p> <blockquote> <p>some message<br> another message<br> test</p> </blockquote> <p>"Data passing" is successful! Eventually, when the WPF window loads I want to pass a <code>List&lt;Session&gt;</code> 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 <code>SessionList</code>).</p> <p>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 <code>Session</code> objects, or whenever a user clicks on a specific one?<br> Any benefit to using MVVM here?</p> <p>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. </p> <p>Edit: for future reference, a decent MVVM tutorial targeted to people like me can be found here: <a href="http://jpreecedev.com/2013/06/08/wpf-mvvm-for-winforms-devs-part-1-of-5/" rel="nofollow noreferrer">http://jpreecedev.com/2013/06/08/wpf-mvvm-for-winforms-devs-part-1-of-5/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload