question

AA-5973 avatar image
0 Votes"
AA-5973 asked AA-5973 commented

Replace dot with comma while input in WPF DataGrid

I am doing a calculator.
How to do a comma here?
My CultureInfo is se-SE

XAML:

 <Window x:Class="Calculator.MainWindow"
         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:Calculator"
         mc:Ignorable="d"
         Title="Calculator" Height="361.313" Width="208.941" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" PreviewKeyDown="Window_KeyDownPreview">
     <Grid HorizontalAlignment="Left" Width="199">
         <Grid.RowDefinitions>
             <RowDefinition/>
             <RowDefinition Height="165*"/>
         </Grid.RowDefinitions>
         <Grid.ColumnDefinitions>
             <ColumnDefinition Width="203*"/>
             <ColumnDefinition Width="6*"/>
         </Grid.ColumnDefinitions>
         <Button x:Name="One" Content="1" HorizontalAlignment="Left" Margin="13,227.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Click" Grid.Row="1"/>
         <Button x:Name="Two" Content="2" HorizontalAlignment="Left" Margin="58,227.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Click" Grid.Row="1"/>
         <Button x:Name="Three" Content="3" HorizontalAlignment="Left" Margin="103,227.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Click" Grid.Row="1"/>
         <Button x:Name="Four" Content="4" HorizontalAlignment="Left" Margin="13,182.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Click" Grid.Row="1"/>
         <Button x:Name="Five" Content="5" HorizontalAlignment="Left" Margin="58,182.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Click" Grid.Row="1"/>
         <Button x:Name="Six" Content="6" HorizontalAlignment="Left" Margin="103,182.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Click" Grid.Row="1"/>
         <Button x:Name="Seven" Content="7" HorizontalAlignment="Left" Margin="13,137.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Click" Grid.Row="1"/>
         <Button x:Name="Eight" Content="8" HorizontalAlignment="Left" Margin="58,137.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Click" Grid.Row="1"/>
         <Button x:Name="Nine" Content="9" HorizontalAlignment="Left" Margin="103,137.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Click" Grid.Row="1"/>
         <Button x:Name="Zero" Content="0" HorizontalAlignment="Left" Margin="13,273.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Click" Grid.Row="1"/>
         <Button x:Name="Plus" Content="+" HorizontalAlignment="Left" Margin="148,227.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Operand" Grid.Row="1" />
         <Button x:Name="Minus" Content="-" HorizontalAlignment="Left" Margin="148,182.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Operand" Grid.Row="1"/>
         <Button x:Name="Multiply" Content="×" HorizontalAlignment="Left" Margin="148,137.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18"  Click="Button_Operand" Grid.Row="1"/>
         <Button x:Name="Equals" Content="=" HorizontalAlignment="Left" Margin="103,273.717,0,0" VerticalAlignment="Top" Width="85" Height="40" FontSize="18" Click="Button_Equals" Grid.Row="1"/>
         <Button x:Name="Dot" Content="." HorizontalAlignment="Left" Margin="58,273.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Click" Grid.Row="1"/>
         <Button x:Name="Clear" Content="C" HorizontalAlignment="Left" Margin="103,92.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Clear_Click" Grid.Row="1"/>
         <Button x:Name="Divide" Content="÷" HorizontalAlignment="Left" Margin="148,92.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18"  Click="Button_Operand" Grid.Row="1"/>
         <Button x:Name="Clear_Copy" Content="CE" HorizontalAlignment="Left" Margin="13,92.717,0,0" VerticalAlignment="Top" Width="85" Height="40" FontSize="18" Click="CE_Click" Grid.Row="1"/>
         <Label x:Name="Equation" Content="" HorizontalAlignment="Left" Margin="12,16.717,0,0" VerticalAlignment="Top" Width="118" Height="31" Grid.Row="1"/>
         <Label x:Name="Display" Content="0" Height="71" Margin="10,16.717,0,0" VerticalAlignment="Top" Width="175" HorizontalAlignment="Left" FontSize="22" HorizontalContentAlignment="Right" BorderThickness="7,0,0,0" VerticalContentAlignment="Center" Grid.Row="1"/>
    
     </Grid>
 </Window>

c#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

 namespace Calculator
 {
     /// <summary>
     /// Interaction logic for MainWindow.xaml
     /// </summary>
     public partial class MainWindow : Window
     {
         Double Value = 0; // Store Decimal and Int Values if needed
         String Operand; // Store the Operand - Will Determine the Maths
         Boolean OperandPressed; 
    
         public MainWindow()
         {
             InitializeComponent();
         }
    
         private void Clear_Click(object sender, RoutedEventArgs e)
         {
             Value = 0;
             Display.Content = "0";
             Equation.Content = "";
         }
    
         private void CE_Click(object sender, RoutedEventArgs e)
         {
             Display.Content = "0";
         }
    
         private void Button_Click(object sender, RoutedEventArgs e)
         {
               
             Button B = (Button)sender; // Cast Generic Object To Buttom
               
             //To Get Rid of the Initial 0 in the Display
             if ((Display.Content.ToString() == "0")||(OperandPressed))
             {
                 Display.Content = " ";
             }
    
             // If Decimal Point already exsists within the Display then do not add another Dedcimal Point
             if (B.Content.ToString() == ".")
             {
                 if(!Display.Content.ToString().Contains("."))
                 {
                     Display.Content = Display.Content.ToString() + B.Content.ToString(); // Append Number onto Display 
                 }
             }
             else
             {
                 Display.Content = Display.Content.ToString() + B.Content.ToString(); // Append Number onto Display 
             }
          
             OperandPressed = false;
         }
    
         private void Button_Operand(object sender, RoutedEventArgs e)
         {
             Button B = (Button)sender; // Cast Obj to Button
    
             // Continously operate on the current results without having to press equals.
             if(Value != 0)
             {
                 Button_Equals(this, null); // emulate the equals button press
                 Operand = B.Content.ToString(); // store operand
                 OperandPressed = true;
                 Equation.Content = Value + " " + Operand; // Update the Equation Label
             }
             else
             {
                 Operand = B.Content.ToString();
                 Value = Double.Parse(Display.Content.ToString()); // Store the First Value
                 OperandPressed = true;
                 Equation.Content = Value + " " + Operand;  
             }
    
         }
    
         private void Button_Equals(object sender, RoutedEventArgs e)
         {
             Equation.Content = " ";
    
             /*Maths is dependent on which operand has been pressed:
              Do the correct maths with first value stored earlier and second value which is currently on the display. */
             switch (Operand)
             {
                 case "+":
                     Display.Content = Value + Double.Parse(Display.Content.ToString());
                     break;
                 case "-":
                     Display.Content = Value - Double.Parse(Display.Content.ToString());
                     break;
                 case "×":
                     Display.Content = Value * Double.Parse(Display.Content.ToString());
                     break;
                 case "÷":
                     Display.Content = Value / Double.Parse(Display.Content.ToString());
                     break;
                 default:
                     break;                 
             }
    
             Value = Double.Parse(Display.Content.ToString()); //Whatever Last one display is stored in Value
             Operand = ""; // Clear Operand once pressed
    
         }
    
         /*Functionality Added to make the calculator work with Keyboard NumberPad
          When the correct Key is detected it will fire off the corresponding button press*/
         private void Window_KeyDownPreview(object sender, KeyEventArgs e)
         {            
             switch (e.Key)
             {
                 case Key.NumPad0:
                     Zero.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.NumPad1:
                     One.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.NumPad2:
                     Two.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.NumPad3:
                     Three.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.NumPad4:
                     Four.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.NumPad5:
                     Five.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.NumPad6:
                     Six.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.NumPad7:
                     Seven.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.NumPad8:
                     Eight.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.NumPad9:
                     Nine.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.Add:
                     Plus.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.Subtract:
                     Minus.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.Multiply:
                     Multiply.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.Divide:
                     Divide.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.Enter:
                     Equals.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;        
             }
         }
     }
 }

dotnet-csharpwindows-wpfdotnet-wpf-xaml
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.

1 Answer

DaisyTian-1203 avatar image
0 Votes"
DaisyTian-1203 answered AA-5973 commented

I added Language="se-SE" for Equation and Display Label in the xaml, and updated the cs code like below:

 public partial class MainWindow : Window
     {
         Double Value = 0; // Store Decimal and Int Values if needed
         String Operand; // Store the Operand - Will Determine the Maths
         Boolean OperandPressed; 
    
         public MainWindow()
         {
             InitializeComponent();
         }
    
         private void Clear_Click(object sender, RoutedEventArgs e)
         {
             Value = 0;
             Display.Content = "0";
             Equation.Content = "";
         }
    
         private void CE_Click(object sender, RoutedEventArgs e)
         {
             Display.Content = "0";
         }
    
         private void Button_Click(object sender, RoutedEventArgs e)
         {
               
             Button B = (Button)sender; // Cast Generic Object To Buttom
               
             //To Get Rid of the Initial 0 in the Display
             if ((Display.Content.ToString() == "0")||(OperandPressed))
             {
                 Display.Content = " ";
             }
    
             // If Decimal Point already exsists within the Display then do not add another Dedcimal Point
             if (B.Content.ToString() == ".")
             {
                 if(!Display.Content.ToString().Contains("."))
                 {
                     Display.Content = Display.Content.ToString() + B.Content.ToString(); // Append Number onto Display 
                     Display.Content = Display.Content.ToString().Replace(".", ","); // Update the Equation Label
                 }
             }
             else
             {
                 Display.Content = Display.Content.ToString() + B.Content.ToString(); // Append Number onto Display 
                 Display.Content = Display.Content.ToString().Replace(".", ","); // Update the Equation Label
             }
    
             OperandPressed = false;
         }
    
         private void Button_Operand(object sender, RoutedEventArgs e)
         {
             Button B = (Button)sender; // Cast Obj to Button
    
             // Continously operate on the current results without having to press equals.
             if(Value != 0)
             {
                 Button_Equals(this, null); // emulate the equals button press
                 Operand = B.Content.ToString(); // store operand
                 OperandPressed = true;
                 Equation.Content = Value + " " + Operand; // Update the Equation Label
                 Equation.Content = Equation.Content.ToString().Replace(".", ","); // Update the Equation Label
             }
             else
             {
                 Operand = B.Content.ToString();
                 Value = Double.Parse(Display.Content.ToString().Replace(",", ".")); // Store the First Value
                 OperandPressed = true;
                 Equation.Content = Value + " " + Operand;  
                 Equation.Content = Equation.Content.ToString().Replace(".", ","); // Update the Equation Label
             }
    
         }
    
         private void Button_Equals(object sender, RoutedEventArgs e)
         {
             Equation.Content = " ";
             string str = "";
    
             /*Maths is dependent on which operand has been pressed:
              Do the correct maths with first value stored earlier and second value which is currently on the display. */
              switch (Operand)
         {
             case "+":
                 Display.Content = Value + Double.Parse(Display.Content.ToString().Replace(",", "."));
                 break;
             case "-":
                 Display.Content = Value - Double.Parse(Display.Content.ToString().Replace(",", "."));
                 break;
             case "×":
                 Display.Content = Value * Double.Parse(Display.Content.ToString().Replace(",", "."));
                 break;
             case "÷":
                 Display.Content = Value / Double.Parse(Display.Content.ToString().Replace(",", "."));
                 break;
             default:
                 break;                 
         }
    
             Value = Double.Parse(Display.Content.ToString()); //Whatever Last one display is stored in Value
             Operand = ""; // Clear Operand once pressed
    
         }
    
         /*Functionality Added to make the calculator work with Keyboard NumberPad
          When the correct Key is detected it will fire off the corresponding button press*/
         private void Window_KeyDownPreview(object sender, KeyEventArgs e)
         {            
             switch (e.Key)
             {
                 case Key.D0:
                     Zero.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.D1:
                     One.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.D2:
                     Two.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.D3:
                     Three.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.D4:
                     Four.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.D5:
                     Five.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.D6:
                     Six.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.D7:
                     Seven.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.D8:
                     Eight.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.D9:
                     Nine.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.Add:
                     Plus.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.Subtract:
                     Minus.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.Multiply:
                     Multiply.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.Divide:
                     Divide.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;
                 case Key.Enter:
                     Equals.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                     break;        
             }
         }
     }

The reuslt picture is:
82237-2.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.


2.gif (60.2 KiB)
· 5
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 copied your code. On the interface, it is still "."
1. How to change it to ","?

  1. When I do the same as you: 12,6 + 3 then it would be 15.6, not 15,6 as your result.
    I do not know why.

  2. Where in XALM should I add Language="se-SE" for Equation and Display


Thanks



0 Votes 0 ·

@AA-5973 Please add it like below:

  <Label x:Name="Equation" Content="" HorizontalAlignment="Left" Margin="12,16.717,0,0" VerticalAlignment="Top" Width="118" Height="31" Grid.Row="1" Language="se-SE"/>
         <Label x:Name="Display" Content="0" Height="71" Margin="10,16.717,0,0" VerticalAlignment="Top" Width="175" HorizontalAlignment="Left" FontSize="22" Language="se-SE" HorizontalContentAlignment="Right" BorderThickness="7,0,0,0" VerticalContentAlignment="Center" Grid.Row="1"/>
0 Votes 0 ·
AA-5973 avatar image AA-5973 DaisyTian-1203 ·

It is working. But how to change dot to coma in the interface?
We still have there a dot.

0 Votes 0 ·
Show more comments