question

pianoboy11-3815 avatar image
0 Votes"
pianoboy11-3815 asked ·

WPF IValueconverter is Not Firing When Click the radio button

Here is my xaml below. The DisplayUnitConverter is not firing when I click the radio buttons. Can someone assist me to see what I am doing wrong? I have initialize the Element name in the SpinEdit control to the Radio Control name.

<Window x:Class=&#34;DisplayUnitClient.MainWindow&#34;
xmlns:dx=&#34;http://schemas.devexpress.com/winfx/2008/xaml/core&#34;
xmlns=&#34;http://schemas.microsoft.com/winfx/2006/xaml/presentation&#34;
xmlns:x=&#34;http://schemas.microsoft.com/winfx/2006/xaml&#34;
xmlns:d=&#34;http://schemas.microsoft.com/expression/blend/2008&#34;
xmlns:mc=&#34;http://schemas.openxmlformats.org/markup-compatibility/2006&#34;
xmlns:dxe=&#34;http://schemas.devexpress.com/winfx/2008/xaml/editors&#34;
xmlns:dxn=&#34;http://schemas.devexpress.com/winfx/2008/xaml/navbar&#34;
xmlns:dxlc=&#34;http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol&#34;
xmlns:DisplayUnitClient =&#34;clr-namespace:DisplayUnitClient&#34;
mc:Ignorable=&#34;d&#34;
Title=&#34;MainWindow&#34; Height=&#34;350&#34; Width=&#34;525&#34;>
<Window.Resources>
<DisplayUnitClient:DisplayUnitConverter x:Key=&#34;DisplayUnitConverter&#34; />
</Window.Resources>
<Grid>
<dxe:SpinEdit x:Name=&#34;spinEditMaxHatchSpeed&#34; Height=&#34;{Binding ActualHeight, ElementName=label}&#34; AllowDefaultButton=&#34;False&#34; Value=&#34;{Binding Path=MaxHatchSpeed, Mode=TwoWay}&#34; Margin=&#34;0,0,0,285&#34;>
<dxe:ButtonInfo IsLeft=&#34;True&#34; Content=&#34;&lt;&#34; Command=&#34;{Binding SpinDownCommand, RelativeSource={RelativeSource AncestorType={x:Type dxe:SpinEdit}, Mode=FindAncestor}}&#34;/>
<dxe:ButtonInfo Content=&#34;&gt;&#34; Command=&#34;{Binding SpinUpCommand, RelativeSource={RelativeSource AncestorType={x:Type dxe:SpinEdit}, Mode=FindAncestor}}&#34;/>
</dxe:SpinEdit>
<GroupBox x:Name=&#34;groupBoxDisplaySettings&#34; Header=&#34;Display Units&#34; HorizontalAlignment=&#34;Stretch&#34; VerticalAlignment=&#34;Top&#34; FontSize=&#34;16&#34; Margin=&#34;0,39,10,0&#34;>
<StackPanel x:Name=&#34;stackPanelDisplayUnit&#34; HorizontalAlignment=&#34;Left&#34; VerticalAlignment=&#34;Center&#34; Orientation=&#34;Vertical&#34; Width=&#34;349&#34; >
<Label Content=&#34;Max Hatch Speed:&#34; />
<RadioButton Margin=&#34;0,0,20,0&#34; Content=&#34;Millimeters&#34; Name=&#34;MillimeterDisplayUnitButton&#34; GroupName=&#34;DisplayUnits&#34; Click=&#34;DisplayUnitMode_Clicked&#34; Tag=&#34;0&#34; IsChecked=&#34;{Binding Path=MaxHatchSpeed, ElementName=spinEditMaxHatchSpeed, Converter={StaticResource DisplayUnitConverter}, Mode=TwoWay}&#34;/>
<RadioButton Content=&#34;Inches&#34; Name=&#34;InchesDisplayUnitButton&#34; GroupName=&#34;DisplayUnits&#34; Click=&#34;DisplayUnitMode_Clicked&#34; Tag=&#34;1&#34; IsChecked=&#34;{Binding Path=MaxHatchSpeed, ElementName=spinEditMaxHatchSpeed, Converter={StaticResource DisplayUnitConverter}, Mode=TwoWay}&#34;/>
</StackPanel>
</GroupBox>
</Grid>
</Window>

windows-wpf
· 1
10 |1000 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.

Please re-put your code in the code block.

0 Votes 0 · ·
LloydSheen-3317 avatar image
0 Votes"
LloydSheen-3317 answered ·

Don't know how you input your XAML but in the forum it is unreadable and that stops us from helping.

· Share
10 |1000 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.

PeterFleischer-3316 avatar image
0 Votes"
PeterFleischer-3316 answered ·

Hi, you bind all CheckBoxes in GroupBox to the same property and use Click event. That can create unpredictable effects. Bind only one Checkbox (in GroupBox) and use setter in binded property instead of click event. Try following demo:

XAML:

 <Window x:Class="Window005"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:local="clr-namespace:WpfApp1.WpfApp005"
         mc:Ignorable="d"
         Title="Window005" Height="450" Width="800">
   <Window.DataContext>
     <local:ViewModel/>
   </Window.DataContext>
   <Window.Resources>
     <local:DisplayUnitConverter x:Key="DisplayUnitConverter" />
   </Window.Resources>
   <StackPanel>
     <GroupBox Header="Display Units" 
               HorizontalAlignment="Stretch" 
               VerticalAlignment="Top" 
               FontSize="16" Margin="0,39,10,0">
       <StackPanel HorizontalAlignment="Left" 
                   VerticalAlignment="Center" 
                   Orientation="Vertical" 
                   Width="349" >
         <Label Content="Max Hatch Speed:" />
         <RadioButton Content="Millimeters" 
                      GroupName="DisplayUnits" 
                      IsChecked="{Binding Path=MaxHatchSpeed, Converter={StaticResource DisplayUnitConverter}, Mode=TwoWay}"/>
         <RadioButton Content="Inches" 
                      GroupName="DisplayUnits"/>
       </StackPanel>
     </GroupBox>
     <StackPanel Orientation="Horizontal" Margin="5">
       <Label Content="MaxHatchSpeed: "/>
       <TextBlock Text="{Binding MaxHatchSpeed}" Margin="3"/>
     </StackPanel>
   </StackPanel>
 </Window> 

Code:

 Imports System.ComponentModel
 Imports System.Globalization
 Imports System.Runtime.CompilerServices
    
 Public Class Window005
    
 End Class
    
 Namespace WpfApp005
   Public Class ViewModel
     Implements INotifyPropertyChanged
    
     Private _maxHatchSpeed As Double = 1
     Public Property MaxHatchSpeed As Double
       Get
         Return Me._maxHatchSpeed
       End Get
       Set(value As Double)
         If value >= 0 AndAlso value <= 100 Then
           Me._maxHatchSpeed = value
           OnPropertyChanged()
         End If
       End Set
     End Property
    
     Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
     Private Sub OnPropertyChanged(<CallerMemberName> Optional propName As String = "")
       RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName))
     End Sub
   End Class
    
   Public Class DisplayUnitConverter
     Implements IValueConverter
    
     Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
       If CType(value, Double) = 1 Then Return True
       Return False
     End Function
    
     Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
       If CType(value, Boolean) Then Return 1
       Return 25.4
     End Function
   End Class
 End Namespace
· Share
10 |1000 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.