WPF IValueconverter is Not Firing When Click the radio button

pianoboy11 1 Reputation point
2020-04-15T12:10:29.503+00:00

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

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,670 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Lloyd Sheen 1,356 Reputation points
    2020-04-15T21:31:02.367+00:00

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

    0 comments No comments

  2. Peter Fleischer (former MVP) 19,231 Reputation points
    2020-04-16T07:12:54.537+00:00

    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
    
    0 comments No comments