Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You approach seems OK to me. It's not perfect, but it is workable enough.</p> <p>An optimal approach, IMO, would be to create a ViewModel for the WPF Window, instead of directly referencing the <code>Window</code> itself when passing data back and forth.</p> <p>The idea is:</p> <pre><code>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(); } } </code></pre> <p>So, the ViewModel would be something like:</p> <pre><code>public class TransactionViewerViewModel : PropertyChangedBase { public ObservableCollection&lt;Session&gt; SessionList { get; set; } public string Test{ get; set; } public TransactionViewerViewModel() { 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" }); } public void AddTest() { SessionList.Add(new Session() { BeginLine = 4, EndLine = 5, Message = Test }); } } </code></pre> <p>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.</p> <p>And, since you're setting the ViewModel as the Window's <code>DataContext</code>, you will need to access all your ViewModel properties via DataBinding, rather than procedural code:</p> <pre><code>&lt;ItemsControl ItemsSource="{Binding SessionList}"/&gt; </code></pre> <p>Then, you may want to introduce <code>delegates</code> or <code>events</code> in the ViewModel, and listen to these in your Form, thus achieving <code>WPF =&gt; winforms</code> communication.</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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