question

RogerSchlueter-7899 avatar image
0 Votes"
RogerSchlueter-7899 asked RogerSchlueter-7899 commented

How to convert RadioButtion values to integers

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?







dotnet-wpf-xaml
radiobutton.png (2.5 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@RogerSchlueter-7899
You need add GroupName for RadioButton to make only one RadioButton IsChecked for the same group. I am little confused, why do you bind Credit and Debit with same TypeID property? and why do you return a integers for IsChecked? Could you tell me what condition you want for IsChecked to be true?

0 Votes 0 ·

The RadioButtons are both in a StackPanel so I don 't need a GroupName.

I want to bind to an integer because the the TypeID property I'm binding to is an integer.

When the Debit RadioButton is checked I want DataContext object to get a 5 and a 206 when it is not checked.

Binding both RadioButtons was a mistake - I have removed one of them.

0 Votes 0 ·

1 Answer

DaisyTian-1203 avatar image
0 Votes"
DaisyTian-1203 answered RogerSchlueter-7899 commented

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.


3.gif (45.2 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I am sure this is correct so I have marked it as the answer. I cannot get my own application to absolutely verify it because I get

System.Windows.Data Error: 40 : BindingExpression path error: 'TypdID' property not found on 'object'

at run-time. Even when I force it to have the right data context by explicitly stating the data context in XAML, I still get the error. All my other controls that share the same data context are working correctly. I am simply baffled and VERY frustrated.

But this problem is outside the scope of my original question so I don't expect you to respond to my whining.




0 Votes 0 ·