Reputation: 1162
I have a control that is bound to a question, that question has many possible options. Each option can have a response.
Example:
Question: Will it fit in a shoe box?
Options: Yes, No, Maybe
Response:
options[0].Response = new Response()
- Meaning that the first option has a response, therefore it has been answered...
For this scenario, I have this represented this using a checkbox. If it is checked, it's answered, if not, response is null. I hope that makes my intention clear.
I am however having problems with the binding in that I can't get the response into the option. I thought I'd be able to use the Convert Parameter but this doesn't seem to work either..
This is how I'm binding to the check box:
<CheckBox Content="{Binding Text}"
IsChecked="{Binding Response,
Mode=TwoWay,
Converter={StaticResource isQuestionResponseNullConverter},
ConverterParameter={Binding Response}}"
/>
So this is the CheckBox with my value converter, it works one way using the following:
public object Convert(object value, Type targetType, object parameter, string language)
{
IResponse response = (IResponse )value;
if (response== null)
{
false;
}
return true;
}
However, I want to bind back using the ConvertBack
method so that if that check box is selected, the binding for that question's response will be that of the CheckBox's binding context. I thought I would be able to do this using the Converter Parameter as shown in the XAML above, however it seems that the parameter is null, am I doing this wrong?
I have read up on this and some sources are saying a DependencyObject is required, I don't understand why however, surely that's what ConvertBack is for?
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
if ((bool)value == true)
{
return parameter; //<-- Always null..
}
else
{
return null;
}
}
Upvotes: 0
Views: 1761
Reputation: 3232
The Converter should be:
public class isQuestionResponseNullConverter : DependencyObject, IValueConverter
{
public object Parameter
{
get { return (object)GetValue(ParameterProperty); }
set { SetValue(ParameterProperty, value); }
}
public static readonly DependencyProperty ParameterProperty =
DependencyProperty.Register("Parameter", typeof(object), typeof(isQuestionResponseNullConverter), new PropertyMetadata(null));
public object Convert(object value, Type targetType, object parameter, string language)
{
IResponse response = (IResponse)value;
if (response == null)
{
return false;
}
return true;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
if ((bool)value == true)
{
return Parameter; //Get Parameter
}
else
{
return null;
}
}
}
And in the XAML
<Page.Resources>
<local:isQuestionResponseNullConverter x:Key="isQuestionResponseNullConverter" Parameter="{Binding Response}"/>
</Page.Resources>
<Grid x:Name="Grid_Master" Background="Red">
<CheckBox VerticalAlignment="Top" Content="{Binding Text}"
IsChecked="{Binding Response, Mode=TwoWay, Converter={StaticResource isQuestionResponseNullConverter}}"/>
</Grid>
And now in the back method you get the Parameter !
I hope you find it easy to use and useful
Upvotes: 1