views:

17

answers:

1

This question relates to this question.

I have a EF model like this.

Using Silverlight 4 I have an application showing questions to the user, defined through the QuestionSet. If a question has more than one answer alternative, a ComboBox is rendered. If only one alternative is given, a TextBox is rendered, in which the user can put freetext.

All user answers are stored in PersonQA entities. But herein lies the question: When the TextBox is rendered, I am not able to find the correct path and bind to the property "AnswerFreetext" (in PersonQA). What am I doing wrong?

XAML:

        <ListBox x:Name="QAListBox" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectedIndex="-1" 
             ItemsSource="{Binding Questions}" IsTabStop="True" TabIndex="5" 
             ScrollViewer.HorizontalScrollBarVisibility="Auto" Margin="10" BorderThickness="0">
            <ListBox.Resources>
                <fx:EqualityToVisibilityConverter x:Key="converter"
                    TrueValue="Visible" FalseValue="Collapsed" />
                <fx:CollectionToFirstElementConverter x:Key="collectiontoitem" />
            </ListBox.Resources>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid d:DesignWidth="931" d:DesignHeight="61" d:IsLocked="True" Margin="0">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width=".80*" MinWidth="800"/>
                            <ColumnDefinition Width=".20*" MinWidth="200"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding Path=QuestionText}" Padding="10" FontSize="21.333" FontWeight="Bold" Margin="0" Grid.Column="0" d:IsLocked="True" />
                        <ComboBox ItemsSource="{Binding AnswerAlternative, Mode=OneWay}" 
                            SelectedValuePath="AnswerAlternativeId" DisplayMemberPath="AnswerText"
                            FontSize="21.333" FontWeight="Bold" Grid.Column="1" Margin="60,0,0,0" d:IsLocked="True" SelectionChanged="ComboBox_SelectionChanged"
                            Visibility="{Binding Path=AnswerAlternative.Count, Converter={StaticResource converter}, ConverterParameter=2}">
                        </ComboBox>
                        <TextBox Grid.Column="1" Margin="60,0,0,0" Text="{Binding Path=PersonQA.AnswerFreetext, Mode=TwoWay}"
                                 Visibility="{Binding AnswerAlternative.Count, Converter={StaticResource converter}, ConverterParameter=1}" />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

The XAML has a ViewModel class as DataContext, which exposes Questions IEnumerable.

t.

A: 

I found an answer to my problem. It solves it by looking at the problem the opposite way. By iterating on the answers in stead of the questions, I am almost there.

In addition I found that by using a ViewModelProxy-class (datacontext proxy) I am able to access the AnswerAlternative collection outside the relative binding inside the datatemplate. Read all about it here: http://weblogs.asp.net/dwahlin/archive/2009/08/20/creating-a-silverlight-datacontext-proxy-to-simplify-data-binding-in-nested-controls.aspx

Thomas