将单个属性绑定到父项源

时间:2012-11-06 11:19:00

标签: c# wpf

我想在ListBox中显示问题列表。每个ListBoxItem包含一个问题和一个带有RadioButtons的ListBox,用于显示答案选项。第一个ListBox的ItemsSource是一个可观察的集合,其对象类为Question。第二个ListBox的ItemsSource是QuestionClass:QuestionObj.Answers中的list属性。 我的问题是将RadioButton属性GroupName绑定到Question属性QuestionObj.Index。我该如何解决这个问题?

以下是课程问题的代码:

public class Question
{
    private static int countQuestions;

    private int index;
    private string questionText;
    private List<String> answers = new List<String>();

    public Question()
    {
        countQuestions++;
    }
    public Question(string type, bool isRequired, string questionText, List<String> answers = null) :this()    
    {
        this.index = countQuestions;
        this.questionText = questionText;
        this.answers = answers;
    }

    public int Index
    {
        get { return index; }
    }
    public string QuestionText
    {
        get { return questionText; }
    }
    public List<String> Answers
    {
        get { return answers; }
    }
}

这里是xaml:

<ListBox x:Name="Questions" ItemsSource="{Binding QuestionList}" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Label Grid.Row="0" Content="{Binding QuestionText}"/>
                <ListBox x:Name="AnswerOptions" Grid.Row="1" ItemsSource="{Binding Answers}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <RadioButton Content="{Binding}" GroupName={Binding ??????}/>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

1 个答案:

答案 0 :(得分:0)

编辑: 将SelectedIndex更改为SelectedItem.Index

<RadioButton Content="{Binding}" 
             GroupName={Binding ElementName=Questions, Path=SelectedItem.Index}/>

看看这里。 www.nbdtech.com/Free/WpfBinding.pdf

相关问题