Following example shows the data binding between radio buttons and ViewModel in WPF.
Parts of View.xaml
<Grid.Resources> <Utilities:RadioHelper x:Key="RadioConverter" /> <Utilities:RadioHelper x:Key="InverseRadioConverter" Inverse="True" /> </Grid.Resources> <RadioButton Name="radFvsOracle" GroupName="fvsStorage" Content="Oracle" Margin="7,0,0,0" IsChecked="{Binding Path=RadFvsResults, Converter= {StaticResource ResourceKey=RadioConverter}}"></RadioButton> <RadioButton Name="radFvsAccess" GroupName="fvsStorage" Content="Access" Grid.Column="2" Margin="11,0,0,0" IsChecked="{Binding Path=RadFvsResults, Converter= {StaticResource ResourceKey=InverseRadioConverter}}"></RadioButton>
Parts of RadioHelper.cs
public class RadioHelper:IValueConverter { public bool Inverse { get; set; } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value != null) { bool boolValue = (bool)value; return this.Inverse ? !boolValue : boolValue; } return Binding.DoNothing; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { bool boolValue = (bool)value; if (!boolValue) { return Binding.DoNothing; } return !this.Inverse; } }
Source- Kept here for future reference.

0 comments :
Post a Comment