Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can bind to the navigation property of entity framework. Use an observable collection for the Company combobox and bind the employee combobox to the navigation property.</p> <p>So in ViewModel you only need:</p> <pre><code>Public Property Companies As ObservableCollection(Of Company) Public Property SelectedCompany As Company Public Property SelectedEmployee As Employee </code></pre> <p>Lets say that your entity framework has a navigation property on your comapny entity named company_employee. You can bind directly to that in your view like:</p> <pre><code>&lt;ComboBox ItemsSource="{Binding SelectedCompany.company_employee}" SelectedItem="{Binding Path=SelectedEmployee,Mode=TwoWay}" /&gt; </code></pre> <p>WPF and entity framework will get the required data for you. and it will only be the employees that have the matching company.</p> <p>WHat I do is use a usercontrol as my view and have a grid/usercontrol/contentcontrol that would bind (set its datacontext) to the SelectedCompany in side my View. </p> <p>ie </p> <pre><code>&lt;ContentControl Name="DetailControl" DataContext="{Binding Path=SelectedCompany, Mode=TwoWay}" &gt; </code></pre> <p>Then within that contentcontrol the binding for a combox or datagrid or listview is as follows if you still want to have a selectedemployee binding:</p> <pre><code>&lt;ComboBox ItemsSource="{Binding company_employee}" SelectedValue="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},Path=DataContext.SelectedEmployee}" /&gt; </code></pre> <p>But if you don't need the selectedEMployee you can just bind to the EMployeeID in the company Table as follows:</p> <pre><code>&lt;ComboBox ItemsSource="{Binding company_employee}" SelectedValue="{Binding EmplyeeID, Mode=TwoWay" /&gt; </code></pre> <p>You just bind directely to the navigation property and use the employee Foreign key.</p> <p>I just typed these out so you might need to double check all the names etc.. Hope these options help</p> <p><strong>EDIT 1:</strong></p> <p>Include example (in vb.net)</p> <pre><code> Dim MyQuery As IQueryable(Of Company) = CType((From MyResults In Context.Company.Include("Employee") Select MyResults), IQueryable(Of Company)) </code></pre> <p>see: <a href="http://msdn.microsoft.com/en-us/library/gg696450%28v=vs.103%29.aspx" rel="nofollow">Include Method MSDN Documentation</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.
    1. This table or related slice is empty.
    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