Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding WPF Toolkit DataGrid ComboBox column to Linq-to-Sql collection after Window loads and connection changes?
    primarykey
    data
    text
    <p>I am a beginner to MVVM. The main window uses a stock DataGrid from the WPF Toolkit. It has one View Model and relies only on DataContext_Changed event handler in the .xaml.cs file. The main window's DataContext is the View Model. </p> <p>I am attempting to bind the ItemsSource property of a DataGridComboBoxColumn to a CLR property, "FieldTypes", on the View Model. "FieldTypes" is a List which derives from a Linq to Sql table of type FieldType. The ItemsSource of the DataGrid is from one System.Data.Linq.Table and "FieldTypes" is from a <em>separate</em> Table, and these two tables have <em>no relationship</em>. </p> <p>To bind the FieldTypes DataGridComboBoxColumn to the "FieldTypes" property on the View Model, I created a DataTemplate StaticResource to be used in a DataGridTemplateColumn:</p> <pre><code> &lt;DataTemplate x:Key='FieldTypeCombo' &gt; &lt;ComboBox DataContext='{Binding ElementName=window, Mode=OneWay, Path=DataContext, UpdateSourceTrigger=PropertyChanged}' ItemsSource='{Binding FieldTypes}' DisplayMemberPath='Name' /&gt; &lt;/DataTemplate&gt; </code></pre> <p>It turns out that this DataTemplate <strong>will</strong> work on a child element of the main window NOT in the DataGrid, such as in a HeaderedContentControl.ContentTemplate, but will <strong>not</strong> work in a DataGridTemplateColumn.CellTemplate.</p> <p>If I try binding a DataGridComboBoxColumn to the ComboBox.ItemsSource property of a regular ComboBox not in the DataGrid, </p> <pre><code> &lt;c:DataGridComboBoxColumn Header="FieldType" ItemsSource="{Binding ElementName=regularcombobox, Path=ItemsSource, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /&gt; </code></pre> <p>it works as expected, but not for a ComboBox inside the DataGrid.</p> <p>I'd like to have as much data binding as possible on the View Model's properties, but this also looks like an opportunity for a StaticResource. This StaticResource would change however, after the window loads, since the connection string is not known until after. Even when binding the ItemsSource of the ComboBox column to the StaticResource, I've had no luck with this, or DynamicResource either.</p> <p>Is there some way to accomplish this binding of a DataGrid ComboBox column to a collection separate from the ItemsSource of the DataGrid, using MVVM? Or is there a way to accomplish this with a StaticResource,even if the SQL connection changes after the window loads?</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. 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