Kenric Nugteren преди 1 месец
родител
ревизия
f128933ec8
променени са 2 файла, в които са добавени 115 реда и са изтрити 0 реда
  1. 32 0
      inabox.wpf/Editors/DecimalEdit.xaml
  2. 83 0
      inabox.wpf/Editors/DecimalEdit.xaml.cs

+ 32 - 0
inabox.wpf/Editors/DecimalEdit.xaml

@@ -0,0 +1,32 @@
+<wpf:ThemableWindow x:Class="InABox.WPF.DecimalEdit"
+        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:sf="http://schemas.syncfusion.com/wpf"
+        xmlns:wpf="clr-namespace:InABox.Wpf"
+        mc:Ignorable="d"
+        Title="DecimalEdit" Height="200" Width="300"
+                    Loaded="ThemableWindow_Loaded"
+                    KeyUp="Editor_KeyUp">
+    <Grid>
+        <Grid.RowDefinitions>
+            <RowDefinition Height="*" />
+            <RowDefinition Height="40" />
+        </Grid.RowDefinitions>
+        <Grid.ColumnDefinitions>
+            <ColumnDefinition Width="Auto" />
+            <ColumnDefinition Width="*" />
+            <ColumnDefinition Width="80" />
+            <ColumnDefinition Width="80" />
+        </Grid.ColumnDefinitions>
+        <Label x:Name="Label" Grid.Row="0" Grid.Column="0" Margin="10" HorizontalAlignment="Left"
+               HorizontalContentAlignment="Left" VerticalAlignment="Stretch" VerticalContentAlignment="Center"
+               Content="Enter Value" />
+        <sf:DoubleTextBox x:Name="Editor" Grid.Row="0" Grid.Column="1" Margin="20" Grid.ColumnSpan="3"
+                            HorizontalAlignment="Stretch" VerticalAlignment="Center" Value="0" TextAlignment="Center"
+                          KeyUp="Editor_KeyUp"/>
+        <Button x:Name="OK" Grid.Row="1" Grid.Column="2" Margin="5" Content="OK" Click="OK_Click" />
+        <Button x:Name="Cancel" Grid.Row="1" Grid.Column="3" Margin="5" Content="Cancel" Click="Cancel_Click" />
+    </Grid>
+</wpf:ThemableWindow>

+ 83 - 0
inabox.wpf/Editors/DecimalEdit.xaml.cs

@@ -0,0 +1,83 @@
+using InABox.Wpf;
+using System;
+using System.Windows;
+
+namespace InABox.WPF
+{
+    /// <summary>
+    ///     Interaction logic for NumberEdit.xaml
+    /// </summary>
+    public partial class DecimalEdit : ThemableWindow
+    {
+        public DecimalEdit(string title, decimal min, decimal max, int decimals, decimal value)
+        {
+            InitializeComponent();
+            Title = title;
+            Value = value;
+            Editor.NumberDecimalDigits = decimals;
+            Editor.MinValue = Convert.ToDouble(min);
+            Editor.MaxValue = Convert.ToDouble(max);
+        }
+
+        public int DecimalPlaces
+        {
+            get => Editor.NumberDecimalDigits;
+            set => Editor.NumberDecimalDigits = value;
+        }
+        
+        public decimal Value
+        {
+            get => Convert.ToDecimal(Editor.Value.HasValue ? Editor.Value.Value : 0);
+            set => Editor.Value = Convert.ToDouble(value);
+        }
+
+        private void Confirm()
+        {
+            if (Editor.Value >= Editor.MinValue && Editor.Value <= Editor.MaxValue)
+            {
+                DialogResult = true;
+                Close();
+            }
+            else
+            {
+                MessageBox.Show($"Value must be in range [{Editor.MinValue}, {Editor.MaxValue}]");
+            }
+        }
+
+        private void OK_Click(object sender, RoutedEventArgs e)
+        {
+            Confirm();
+        }
+
+        private void Cancel_Click(object sender, RoutedEventArgs e)
+        {
+            DialogResult = false;
+            Close();
+        }
+
+        public static bool Execute(string title, decimal min, decimal max, int decimals, ref decimal value)
+        {
+            var edit = new DecimalEdit(title, min, max, decimals, value);
+            if (edit.ShowDialog() == true)
+            {
+                value = edit.Value;
+                return true;
+            }
+
+            return false;
+        }
+
+        private void ThemableWindow_Loaded(object sender, RoutedEventArgs e)
+        {
+            Editor.Focus();
+        }
+
+        private void Editor_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
+        {
+            if(e.Key == System.Windows.Input.Key.Enter)
+            {
+                Confirm();
+            }
+        }
+    }
+}