Note that there are some explanatory texts on larger screens.

plurals
  1. POBind SilverLight 5 DataGrid column value to an element in a collection based on a combox box selection
    text
    copied!<p>I have a silverlight viewmodel part of which looks something like this:</p> <pre><code>class ViewModel { ObservableCollection&lt;Person&gt; People; ObservableCollection&lt;Question&gt; Questions; int SelectedQuestionID; } class Person { int ID; string Name; int Age; string Gender; Dictionary&lt;int, Question&gt; Answers; } class QandA { int ID; string QuestionOrAnswerText; } </code></pre> <p>I want to bind an ObservableCollection of "Person" objects to a DataGrid and show some columns. Three of the columns are easy: Name, Age, Gender. The fourth is a little more tricky. I would like to have fourth column that contains the "QuestionOrAnswerText" of one of the questions in the Person.Answers property that corresponds to the selectedvalue of a combobox in the column header for that fourth column and that changes when the user changes the selected value in the aforementioned combobox. The combobox's selectedvalue property would be bound to my viewmodel's SelectedQuestionID property and I would like to be able to somehow bind to that property in my datagrid column to get at my dictionary value.</p> <p>I'm having a very hard time figuring out what binding path to use in this case.</p> <p>Here is what my XAML looks like for this column:</p> <pre><code> &lt;sdk:DataGridTemplateColumn&gt; &lt;sdk:DataGridTemplateColumn.HeaderStyle&gt; &lt;Style TargetType="dataprimitives:DataGridColumnHeader"&gt; &lt;Setter Property="ContentTemplate"&gt; &lt;Setter.Value&gt; &lt;DataTemplate&gt; &lt;ComboBox DataContext="{Binding SelectedDivision}" ItemsSource="{Binding Questions}" SelectedValue="{Binding SelectedQuestion, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" SelectedValuePath="ID" DisplayMemberPath="Text"&gt;&lt;/ComboBox&gt; &lt;/DataTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/sdk:DataGridTemplateColumn.HeaderStyle&gt; &lt;sdk:DataGridTemplateColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding Path=Answers[{Binding SelectedQuestion, RelativeSource={RelativeSource TemplatedParent}}].Text}"&gt;&lt;/TextBlock&gt; &lt;/DataTemplate&gt; &lt;/sdk:DataGridTemplateColumn.CellTemplate&gt; &lt;/sdk:DataGridTemplateColumn&gt; </code></pre> <p>The XAML editor says that I have an issue in the {Binding Path=Answers[..].Text} and it's expecting an extra closing curly brace after the ".Text" part. However, that would unbalance the brackets.</p> <p>Thanks, Matthew</p>
 

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