locked
Binding and save radiobutton value RRS feed

  • Question

  • Hi All.

    I have problem to save value of radiobutton to dataset

    In XAML

    <RadioButton Name="PhoneRadioButton" GroupName="Method"
     IsChecked="{Binding Path=Field2, Mode=TwoWay,
     ConverterParameter=true}" Grid.Column="2"
     Foreground="#FF4D4D4D" FontWeight="Normal"
     FontStretch="Normal" FontFamily="Verdana" FontSize="11"
     VerticalAlignment="Center" HorizontalAlignment="Left"
     Width="150">Phone
    </RadioButton>

    In VB code for Save button procedure I try to save string value if radiobutton checked

    If (PhoneRadioButton.IsChecked) Then
     Field3 = "By Phone"
    End If
    

     

    What is wrong?

    Thanks




    Thursday, September 1, 2011 4:08 PM

Answers

  • Hi eugzl,
    I made a small demo on how converter could be used in this case.
    namespace WpfApplication1.Converters
    {
       public class Converters : System.Windows.Data.IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                string str = (string)value;
                if (str == "By Phone" && parameter.ToString() == "phone") return true;
                if (str == "By Others" && parameter.ToString() == "other") return true;
                else return false;
            }
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                bool isChecked=false;
                if (value != null)
                    isChecked = (bool)value;
                if (isChecked && parameter.ToString()=="phone") return "By Phone";
                if (isChecked && parameter.ToString() == "other") return "By Others";
                else return "By Others";
            }
        }
    }
    
    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for Window2.xaml
        /// </summary>
        public partial class Window2 : Window
        {
            Item item = new Item();
            public Window2()
            {
                InitializeComponent();
                this.DataContext = item;
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                MessageBox.Show(item.Field);
            }
        }
        public class Item
        {
            public string Field
            {
                get;
                set;
            }
        }
       
    }
    

    Xaml:
    <Window x:Class="WpfApplication1.Window2"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:converter="clr-namespace:WpfApplication1.Converters"
            Title="Window2"  Height="300" Width="300">
        <StackPanel>
            <StackPanel.Resources >
                <converter:Converters x:Key="BooleanToStringValueConverter"/>
            </StackPanel.Resources>
            <RadioButton Content="By Phone"  IsChecked="{Binding Path=Field, Mode=TwoWay, Converter={StaticResource BooleanToStringValueConverter}, ConverterParameter=phone}"/>
            <RadioButton Content="By Others" IsChecked="{Binding Path=Field, Mode=TwoWay, Converter={StaticResource BooleanToStringValueConverter}, ConverterParameter=other}"/>
            <Button Content="Button" Click="button1_Click" />
        </StackPanel>
    </Window>
    
    

    Best Regards,

    Kee Poppy [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Kee Poppy Friday, September 2, 2011 10:54 AM
    • Marked as answer by Jie Bao Thursday, September 8, 2011 3:18 AM
    Friday, September 2, 2011 10:52 AM
  • It looks like the XAML is using some value converter, could you give the detials for the converter?

    e.g. Does the converter can convert boolean <> string ?

    • Proposed as answer by Kee Poppy Friday, September 2, 2011 10:53 AM
    • Marked as answer by Jie Bao Thursday, September 8, 2011 3:17 AM
    Thursday, September 1, 2011 10:51 PM

All replies

  • It looks like the XAML is using some value converter, could you give the detials for the converter?

    e.g. Does the converter can convert boolean <> string ?

    • Proposed as answer by Kee Poppy Friday, September 2, 2011 10:53 AM
    • Marked as answer by Jie Bao Thursday, September 8, 2011 3:17 AM
    Thursday, September 1, 2011 10:51 PM
  • Hi eugzl,
    I made a small demo on how converter could be used in this case.
    namespace WpfApplication1.Converters
    {
       public class Converters : System.Windows.Data.IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                string str = (string)value;
                if (str == "By Phone" && parameter.ToString() == "phone") return true;
                if (str == "By Others" && parameter.ToString() == "other") return true;
                else return false;
            }
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                bool isChecked=false;
                if (value != null)
                    isChecked = (bool)value;
                if (isChecked && parameter.ToString()=="phone") return "By Phone";
                if (isChecked && parameter.ToString() == "other") return "By Others";
                else return "By Others";
            }
        }
    }
    
    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for Window2.xaml
        /// </summary>
        public partial class Window2 : Window
        {
            Item item = new Item();
            public Window2()
            {
                InitializeComponent();
                this.DataContext = item;
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                MessageBox.Show(item.Field);
            }
        }
        public class Item
        {
            public string Field
            {
                get;
                set;
            }
        }
       
    }
    

    Xaml:
    <Window x:Class="WpfApplication1.Window2"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:converter="clr-namespace:WpfApplication1.Converters"
            Title="Window2"  Height="300" Width="300">
        <StackPanel>
            <StackPanel.Resources >
                <converter:Converters x:Key="BooleanToStringValueConverter"/>
            </StackPanel.Resources>
            <RadioButton Content="By Phone"  IsChecked="{Binding Path=Field, Mode=TwoWay, Converter={StaticResource BooleanToStringValueConverter}, ConverterParameter=phone}"/>
            <RadioButton Content="By Others" IsChecked="{Binding Path=Field, Mode=TwoWay, Converter={StaticResource BooleanToStringValueConverter}, ConverterParameter=other}"/>
            <Button Content="Button" Click="button1_Click" />
        </StackPanel>
    </Window>
    
    

    Best Regards,

    Kee Poppy [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Kee Poppy Friday, September 2, 2011 10:54 AM
    • Marked as answer by Jie Bao Thursday, September 8, 2011 3:18 AM
    Friday, September 2, 2011 10:52 AM