Thursday, August 18, 2016

Code snippet: Data binding between ViewModel and Radio Button in WPF

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.

C# , WPF

0 comments :

Post a Comment

 

© 2011 GIS and Remote Sensing Tools, Tips and more .. ToS | Privacy Policy | Sitemap

About Me