Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using MVVM, for functionality like this, we work with the view model rather than the views. First add a property of the same type as the items in the <code>ComboBox</code> control... I'm going to assume that they are <code>string</code> values for simplicity's sake:</p> <pre><code>private string selectedItem = string.Empty; ... public string SelectedItem { get { return selectedItem; } set { selectedItem = value; NotifyPropertyChange("SelectedItem"); // &lt;&lt; Implement INotifyPropertyChanged UpdateGrid(); } } </code></pre> <p>Now add a collection property for your <code>DataGrid</code>:</p> <pre><code>private ObservableCollection&lt;string&gt; items = new ObservableCollection&lt;string&gt;(); ... public ObservableCollection&lt;string&gt; Items { get { return items; } set { items= value; NotifyPropertyChange("Items"); // &lt;&lt; Implement INotifyPropertyChanged } } </code></pre> <p>Now let's look at that <code>UpdateGrid</code> method:</p> <pre><code>private void UpdateGrid() { // Update your property that is bound to the DataGrid.ItemsSource here Items = GetNewData(); // &lt;&lt; You need to implement this method yourself } </code></pre> <p>Now, in XAML:</p> <pre><code>&lt;DataGrid ItemsSource="{Binding Items}" /&gt; &lt;ComboBox ItemsSource="{Binding YourCollectionNotShownHere}" SelectedItem="{Binding SelectedItem}" /&gt; </code></pre> <p>Now whenever the <code>ComboBox.SelectedItem</code> changes, the <code>SelectedItem</code> property setter will be called and that will call the <code>UpdateGrid()</code> method, which will update the <code>Items</code> collection property and that will update the collection in the <code>DataGrid</code>. As the comment in the example said, you <em>will</em> need to implement the <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx" rel="nofollow"><code>INotifyPropertyChanged</code></a> interface in your view model.</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.
 

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