Note that there are some explanatory texts on larger screens.

plurals
  1. POSilverlight 4 - Binding ComboBox in DataGrid to property of WCF Client Contract class
    primarykey
    data
    text
    <p>I have a scenario that does not seem to be covered in any of the other similar questions (most of which are unanswered anyways).</p> <p>Here goes:</p> <p>I have the following items to work with:</p> <p>1) A WCF Service that returns a very complex data contract with over 100 properties. I do not wish to create a ViewModel of this object as I don't want to have to maintain over 100 properties <em>unless absolutely necessary</em>.</p> <p>2) I have a Silverlight UserControl that has this data contract defined as a Resource:</p> <pre><code>&lt;UserControl.Resources&gt; &lt;DieEstimateService:DieEstimateContract x:Key="Die" /&gt; &lt;/UserControl.Resources&gt; </code></pre> <p>3) I have a datagrid that binds to a collection of the Die called Die.DieMetalforming. This collection is an Array of DieMetalformingContract objects. There are two columns to display from this collection, the DiePunchFunctionDescription and the DiePunchConstructionDescription properties. The DiePunchFunctionDescription has a corresponding key properties called DiePunchFunctionUID and likewise, the DiePunchConstructionDescription has a key property called DiePunchConstructionUID. When the grid is in display mode, it uses TextBlocks to display the DiePunchFunctionDescription and DiePunchConstructionDescription fields. The DataGrid is bound to the Die.DieMetalforming array via code behind as such:</p> <pre><code>dgDieMetalforming.ItemsSource = Die.DieMetalformings; </code></pre> <p>4) When the grid is in edit mode, there are comboboxes for the DiePunchFunction and DiePunchConstruction columns, both of which are to be bound to the collections of metadata from the Die object. These collections are named DiePunchFunctionFields and DiePunchConstructionFields. Both collections are arrays of StandardCostFieldContract objects (they just have different data in them based on the type of field). The StandardCostFieldContract has a display property of "FieldDescription" and a key property of "StandardCostFieldUID".</p> <p>5) Finally, the datagrid contains a third column on the Quantity property of the DieMetalformingContract. In normal mode this is a TextBlock and in edit mode this is a TextBox. This property is working as intended for two-way databinding.</p> <p>The datagrid binds correctly to the datasource as evidenced by the correct functionality of the TextBlocks and the TextBox of the Quantity field. However, I cannot get the comboboxes to load their data from the DiePunchFunctions or DiePunchConstructions arrays. I've ready a dozen articles on the subject and tried every permutation of ItemSources binding I could find but nothing seems to work. Here's my datagrid definition:</p> <pre><code> &lt;sdk:DataGrid AutoGenerateColumns="False" Height="167" HorizontalAlignment="Stretch" Margin="6,255,6,0" Name="dgDieMetalforming" VerticalAlignment="Top" Width="Auto" Grid.ColumnSpan="4" SelectionMode="Single" TabNavigation="Local"&gt; &lt;sdk:DataGrid.Columns&gt; &lt;sdk:DataGridTemplateColumn CanUserReorder="False" CanUserResize="False" CanUserSort="False" Header="Punch/Die Function" Width="Auto" &gt; &lt;sdk:DataGridTemplateColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding DiePunchFunctionDescription}" Margin="4" /&gt; &lt;/DataTemplate&gt; &lt;/sdk:DataGridTemplateColumn.CellTemplate&gt; &lt;sdk:DataGridTemplateColumn.CellEditingTemplate&gt; &lt;DataTemplate&gt; &lt;ComboBox Name="ddlDiePunchFunction" ItemsSource="{Binding Path=Die.DiePunchFunctionFields, Mode=OneWay}" SelectedItem="{Binding Path=DiePunchFunctionUID, Mode=TwoWay}" DisplayMemberPath="FieldDescription" SelectedValuePath="StandardCostFieldUID" /&gt; &lt;/DataTemplate&gt; &lt;/sdk:DataGridTemplateColumn.CellEditingTemplate&gt; &lt;/sdk:DataGridTemplateColumn&gt; &lt;sdk:DataGridTemplateColumn CanUserReorder="False" CanUserResize="False" CanUserSort="False" Header="Punch/Die Construction" Width="Auto" &gt; &lt;sdk:DataGridTemplateColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding DiePunchConstructionDescription}" Margin="4" /&gt; &lt;/DataTemplate&gt; &lt;/sdk:DataGridTemplateColumn.CellTemplate&gt; &lt;sdk:DataGridTemplateColumn.CellEditingTemplate&gt; &lt;DataTemplate&gt; &lt;ComboBox Name="ddlDiePunchFunction" ItemsSource="{Binding Path=Die.DiePunchConstructionFields, Mode=OneWay}" SelectedItem="{Binding Path=DiePunchConstructionUID, Mode=TwoWay}" DisplayMemberPath="FieldDescription" SelectedValuePath="StandardCostFieldUID" /&gt; &lt;/DataTemplate&gt; &lt;/sdk:DataGridTemplateColumn.CellEditingTemplate&gt; &lt;/sdk:DataGridTemplateColumn&gt; &lt;sdk:DataGridTemplateColumn CanUserReorder="False" CanUserResize="False" CanUserSort="False" Header="Quantity" Width="Auto" &gt; &lt;sdk:DataGridTemplateColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding Quantity}" Margin="4" /&gt; &lt;/DataTemplate&gt; &lt;/sdk:DataGridTemplateColumn.CellTemplate&gt; &lt;sdk:DataGridTemplateColumn.CellEditingTemplate&gt; &lt;DataTemplate&gt; &lt;TextBox Name="txtQuantity" Text="{Binding Quantity, Mode=TwoWay}" /&gt; &lt;/DataTemplate&gt; &lt;/sdk:DataGridTemplateColumn.CellEditingTemplate&gt; &lt;/sdk:DataGridTemplateColumn&gt; &lt;/sdk:DataGrid.Columns&gt; &lt;/sdk:DataGrid&gt; </code></pre> <p>I've spent way too much time on this already and it's reached a point where I need to rework the whole idea before billing for any more time on this problem. If you can recommend a course of action that will make the comboboxes bind to their itemsources I would be eternally grateful.</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.
 

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