How to convert RadioButtion values to integers

RogerSchlueter-7899 1,256 Reputation points
2021-06-08T08:23:28.853+00:00

I have two RadioButtons on a wpf window that the user can use to indicate whether a transaction is of type credit or debit. The DataContext of the window is a transaction with an integer property TypeID. I am trying to bind the RadioButtons to this property using a converter in XAML. Here's the relevant portion of my XAML:

<StackPanel  
    Orientation="Horizontal">  
    <RadioButton Content="Credit"  
    	IsChecked="{Binding Path=TypeID, Converter={StaticResource conBooleanToInt}}" />  
    <RadioButton  
    	Content="Debit"  
    	IsChecked="{Binding Path=TypeID, Converter={StaticResource conBooleanToInt}}"  
    	Margin="15,0,0,0">  
    </RadioButton>  
</StackPanel>  

and here is the BooleanToInt converter:

Public Class BooleanToInt  
	Implements IValueConverter  
  
	Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert  
		Dim x As Boolean = CBool(value)  
		Console.WriteLine($"Convert {x}")  
		Return If(x, 5, 209)  
	End Function  
  
	Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack  
		Dim x As Boolean = CBool(value)  
		Console.WriteLine($"Back {x}")  
		Return If(x, 5, 209)  
	End Function  
End Class  

When the window is loaded here is what it looks like:

103363-radiobutton.png

and here is the output of the Console statements:

Convert False  
Convert False  
Back False  
Convert True  
Convert True  

Obviously, this is not working:

  • Why is the converter called five times, yielding different results
  • Why do both RadioButtons have IsClicked = True (which should not be possible)

and the ultimate question:

  • How to do this?
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
789 questions
{count} votes

Accepted answer
  1. DaisyTian-1203 11,621 Reputation points
    2021-06-09T07:19:44.31+00:00

    I make a sample based on my understanding, please check if it meets your needs, if it doesn't, let me know:
    XAML code is:

       <Window.Resources>  
            <local:BooleanToInt x:Key="conBooleanToInt" />  
        </Window.Resources>  
        <StackPanel Orientation="Horizontal">  
            <Label Name="lb" Width="40" Height="30" Content="{Binding TypeID,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" Background="Azure" VerticalAlignment="Top" ></Label>  
            <RadioButton Content="Credit" IsChecked="True" ></RadioButton>  
            <RadioButton Content="Debit" IsChecked="{Binding Path=TypeID, Converter={StaticResource conBooleanToInt},UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" Margin="30 0 0 0"/>  
        </StackPanel>  
    

    vb code is:

    Imports System.ComponentModel  
      
    Partial Public Class MainWindow  
        Inherits Window  
      
        Private Property model As Model = New Model()  
      
        Public Sub New()  
            InitializeComponent()  
            model.TypeID = 5  
            Me.DataContext = model  
        End Sub  
    End Class  
      
    Public Class Model  
        Implements INotifyPropertyChanged  
      
        Public _typeID As Integer  
      
        Public Property TypeID As Integer  
            Get  
                Return _typeID  
            End Get  
            Set(ByVal value As Integer)  
                _typeID = value  
                OnPropertyChanged("TypeID")  
            End Set  
        End Property  
      
        Private Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged  
      
        Protected Sub OnPropertyChanged(ByVal proName As String)  
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(proName))  
        End Sub  
    End Class  
      
      
      
    Public Class BooleanToInt  
    Implements IValueConverter  
      
        Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert  
            Dim x As Integer = Integer.Parse(value.ToString())  
      
            If x = 5 Then  
                Return True  
            Else  
                Return False  
            End If  
        End Function  
      
      
        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack  
            Dim x As Boolean = System.Convert.ToBoolean(value)  
      
            If x = False Then  
                Return 209  
            Else  
                Return 5  
            End If  
        End Function  
    End Class  
      
    

    Result picture is:
    103781-3.gif


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful