I have some XAML that looks (trimmed) like this, with a button tying its IsEnabled
attribute to a subproperty:
<Grid DataContext="{Binding RelativeSource={RelativeSource AncestorType=Window}}">
...
<Button x:Name="continueButton" Content="Continue" IsEnabled="{Binding CurrentQuestion.AnswerSelected, Mode=OneWay}" Click="continueButton_Click"/>
...
CurrentQuestion
is a property that pulls the current one from a collection:
public Question CurrentQuestion {
get
{
return Questions[QuestionNo];
}
set
{
Questions[QuestionNo] = value;
}
}
AnswerSelected
checks whether any of the Answer
s are marked as selected.
public bool AnswerSelected
{
get
{
return Answers.Any(a => a.Selected);
}
}
The Selected
property itself is set by radio buttons bound to the possible answers. Thus, the user should be able to continue after choosing an answer.
The Answers
collection is monitored for changes, and calls the OnPropertyChanged()
method from INotifyPropertyChanged
for the AnswerSelected
bool property like so:
public Answer[] Answers
{
get
{
return _answers;
}
set
{
_answers = value;
OnPropertyChanged("Answers");
OnPropertyChanged("AnswerSelected");
}
}
The binding successfully sets the button to disabled to begin with, but no change to the radio buttons then re-enables the button. I tried moving AnswerSelected
to be a property at the same level as CurrentQuestion
but this didn't work either.
What am I missing to get this button to re-enable? Also, is there a better way to accomplish this same thing?
Edit:
This is the code for the radio button setting.
<Grid DataContext="{Binding CurrentQuestion, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
<ItemsControl ItemsSource="{Binding Answers}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<RadioButton GroupName="AnswerGroup" IsChecked="{Binding Selected}">
<TextBlock Text="{Binding Text}" />
</RadioButton>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
So it looks like this:
CurrentAnswer (Questions[QuestionNo])
AnswerSelected (Answers.Any(a => a.Selected))
Edit 2:
I think my question, effectively, is this: The bound property is a calculated property, but the calculation uses the subproperty of an array element. How do I, therefore, raise notification when that subproperty is changed, which itself is in a different class that defines each array element?