Browse Source

自定义控件-合并

liyijie 2 years ago
parent
commit
dec19ae3a4

+ 32 - 0
PDF Office/CustomControl/CommonWritableComboBox.xaml

@@ -0,0 +1,32 @@
+<UserControl x:Class="PDF_Office.CustomControl.CommonWritableComboBox"
+             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
+             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
+             xmlns:local="clr-namespace:PDF_Office.CustomControl"
+             mc:Ignorable="d" 
+              d:DesignHeight="32" d:DesignWidth="66">
+    <Grid>
+        <Grid.ColumnDefinitions>
+            <ColumnDefinition Width="*"></ColumnDefinition>
+            <ColumnDefinition Width="16"></ColumnDefinition>
+        </Grid.ColumnDefinitions>
+        <TextBox Name="TextBox_Num"
+                MinHeight="32"
+                Grid.Column="0"
+                Padding="16,0,0,0"
+                VerticalContentAlignment="Center"
+                Background="Transparent"
+                BorderThickness="0"
+                FontFamily="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:NumericUpDown}, Path=FontFamily}"
+                FontSize="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:NumericUpDown}, Path=FontSize}"
+                Foreground="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:NumericUpDown}, Path=Foreground}"
+                InputMethod.IsInputMethodEnabled="False"
+                PreviewTextInput="CountTextBox_PreviewTextInput"
+                Text="1"
+                TextAlignment="Left"
+                KeyDown="CurrentPage_KeyDown"
+                TextChanged="TextBox_Num_TextChanged" />
+        <ComboBox Name="ComboBox_Type" MinHeight="32" Width="16" HorizontalAlignment="Right" BorderBrush="Transparent" BorderThickness="00" Grid.Column="1" SelectionChanged="ComboBox_SelectionChanged"></ComboBox>
+    </Grid>
+</UserControl>

+ 148 - 0
PDF Office/CustomControl/CommonWritableComboBox.xaml.cs

@@ -0,0 +1,148 @@
+using ImTools;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Text.RegularExpressions;
+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 PDF_Office.CustomControl
+{
+    /// <summary>
+    /// CommonWritableComboBox.xaml 的交互逻辑
+    /// </summary>
+    public partial class CommonWritableComboBox : UserControl
+    {
+        public CommonWritableComboBox()
+        {
+            InitializeComponent();
+        }
+        public string Text
+        {
+            get { return (string)GetValue(TextProperty); }
+            set { SetValue(TextProperty, value); }
+        }
+
+        public static readonly DependencyProperty TextProperty =
+            DependencyProperty.Register("Text", typeof(string), typeof(CommonWritableComboBox), new PropertyMetadata(""));
+
+
+        /// <summary>
+        /// 当前值
+        /// </summary>
+        public double Value
+        {
+            get { return (double)GetValue(ValueProperty); }
+            set
+            {
+                if (value > Maximum)
+                {
+                    SetValue(ValueProperty, Maximum);
+                }
+                else if (value < Minimum)
+                {
+                    SetValue(ValueProperty, Minimum);
+                }
+                else
+                {
+                    SetValue(ValueProperty, value);
+                    //TextBox_Num.Text = Value.ToString();
+                }
+            }
+        }
+
+        public static readonly DependencyProperty ValueProperty =
+            DependencyProperty.Register("Value", typeof(double), typeof(CommonWritableComboBox), new PropertyMetadata(0.0));
+
+        public List<string> TypeSouce
+        {
+            get { return (List<string>)GetValue(TypeSouceProperty); }
+            set { SetValue(TypeSouceProperty, value); }
+        }
+
+        public static readonly DependencyProperty TypeSouceProperty =
+            DependencyProperty.Register("TypeSouce", typeof(List<string>), typeof(CommonWritableComboBox), new PropertyMetadata(new List<string>()));
+
+        /// <summary>
+        /// 最大值
+        /// </summary>
+        public double Maximum
+        {
+            get { return (double)GetValue(MaximumProperty); }
+            set { SetValue(MaximumProperty, value); }
+        }
+
+        public static readonly DependencyProperty MaximumProperty =
+            DependencyProperty.Register("Maximum", typeof(double), typeof(CommonWritableComboBox), new PropertyMetadata(9999.0));
+
+        
+        /// <summary>
+        /// 最小值
+        /// </summary>
+        public double Minimum
+        {
+            get { return (double)GetValue(MinimumProperty); }
+            set { SetValue(MinimumProperty, value); }
+        }
+
+        public static readonly DependencyProperty MinimumProperty =
+            DependencyProperty.Register("Minimum", typeof(double), typeof(CommonWritableComboBox), new PropertyMetadata(0.0));
+
+        /// <summary>
+        /// 单位符号
+        /// </summary>
+        public string Unit
+        {
+            get { return (string)GetValue(UnitProperty); }
+            set { SetValue(UnitProperty, value); }
+        }
+
+        public static readonly DependencyProperty UnitProperty =
+            DependencyProperty.Register("Unit", typeof(string), typeof(CommonWritableComboBox), new PropertyMetadata("%"));
+
+        private void TextBox_Num_TextChanged(object sender, TextChangedEventArgs e)
+        {
+            
+            if (this.TextBox_Num.Text == "" )
+            {
+                this.TextBox_Num.Text ="0";
+            }
+
+            if (!int.TryParse(this.TextBox_Num.Text, out _)&&!int.TryParse(this.TextBox_Num.Text.Replace(Unit, ""), out _)) { this.TextBox_Num.Text = "0"; }
+            else{ 
+            Text = this.TextBox_Num.Text;
+            Value = int.Parse(this.TextBox_Num.Text.Replace(" ", "").Replace(Unit, ""));
+            }
+        }
+
+        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
+        {
+
+            this.TextBox_Num.Text = TypeSouce[ComboBox_Type.SelectedIndex];
+            Text = this.TextBox_Num.Text;
+            Value = int.Parse(this.TextBox_Num.Text.Replace(" ", "").Replace(Unit, ""));
+        }
+        private void CountTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
+        {
+            //e.Handled = new Regex("[^0-9]+").IsMatch(e.Text);
+        }
+
+        private void CurrentPage_KeyDown(object sender, KeyEventArgs e)
+        {
+            if (e.Key == Key.Enter)
+            {
+                this.TextBox_Num.Text = this.TextBox_Num.Text.Replace(" " , "").Replace(Unit,"")+" "+Unit;
+                
+            }
+        }
+    }
+}

+ 67 - 26
PDF Office/CustomControl/NumericUpDown.xaml

@@ -1,28 +1,69 @@
-<UserControl x:Class="PDF_Office.CustomControl.NumericUpDown"
-            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
-             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
-             xmlns:local="clr-namespace:PDF_Office.CustomControl"
-             mc:Ignorable="d" 
-             d:DesignHeight="40" d:DesignWidth="80">
-    <Grid>
-        <Grid.ColumnDefinitions>
-            <ColumnDefinition Width="4*"/>
-            <ColumnDefinition Width="1*"/>
-        </Grid.ColumnDefinitions>
-        <TextBox Name="TextBox_Num" Grid.Column="0" Text="1" FontSize="20" TextAlignment="Center" MinWidth="80" MinHeight="20" VerticalContentAlignment="Center" TextChanged="TextBox_Num_TextChanged" InputMethod.IsInputMethodEnabled="False"  PreviewTextInput="CountTextBox_PreviewTextInput" />
-        <Grid Grid.Column="1">
-            <Grid.RowDefinitions>
-                <RowDefinition/>
-                <RowDefinition/>
-            </Grid.RowDefinitions>
-            <Button Name="Button_Add" Grid.Row="0" Click="Button_Add_Click" Background="White">
-                <Path  VerticalAlignment="Center" HorizontalAlignment="Center" Stroke="Black"  Data="M0 8L4 1 L8 8" Height="10" Stretch="Fill" Visibility="{Binding CompressLargeStyle}"/>
-            </Button>
-            <Button Name="Button_Sub" Grid.Row="1"   Click="Button_Sub_Click" Background="White">
-                <Path  VerticalAlignment="Center" HorizontalAlignment="Center" Stroke="Black"  Data="M0 8L4 16 L8 8" Height="10" Stretch="Fill" Visibility="{Binding CompressLargeStyle}"/>
-            </Button>
+<UserControl
+    x:Class="PDF_Office.CustomControl.NumericUpDown"
+    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:local="clr-namespace:PDF_Office.CustomControl"
+    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+    d:DesignHeight="40"
+    d:DesignWidth="80"
+    Background="#EEEEEE"
+    FontSize="20"
+    mc:Ignorable="d">
+    <Border>
+        <Grid>
+            <Grid.ColumnDefinitions>
+                <ColumnDefinition Width="*" />
+                <ColumnDefinition Width="auto" />
+            </Grid.ColumnDefinitions>
+            <TextBox
+                Name="TextBox_Num"
+                MinWidth="80"
+                MinHeight="20"
+                Padding="16,0,0,0"
+                VerticalContentAlignment="Center"
+                Background="Transparent"
+                BorderThickness="0"
+                FontFamily="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:NumericUpDown}, Path=FontFamily}"
+                FontSize="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:NumericUpDown}, Path=FontSize}"
+                Foreground="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:NumericUpDown}, Path=Foreground}"
+                InputMethod.IsInputMethodEnabled="False"
+                PreviewTextInput="CountTextBox_PreviewTextInput"
+                Text="1"
+                TextAlignment="Left"
+                TextChanged="TextBox_Num_TextChanged" />
+            <Grid Grid.Column="1" Width="20">
+                <Grid.RowDefinitions>
+                    <RowDefinition />
+                    <RowDefinition />
+                </Grid.RowDefinitions>
+                <Button
+                    Name="Button_Add"
+                    Grid.Row="0"
+                    Background="Transparent"
+                    BorderBrush="Transparent"
+                    BorderThickness="0"
+                    Click="Button_Add_Click">
+                    <Path
+                        HorizontalAlignment="Center"
+                        Data="M8 4.29102L2.14648 10.1445L2.85547 10.8535L8 5.70898L13.1445 10.8535L13.8535 10.1445L8 4.29102Z"
+                        Fill="Black"
+                        Stretch="Uniform"
+                        Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:NumericUpDown}, Path=ShowBtn}" />
+                </Button>
+                <Button
+                    Name="Button_Sub"
+                    Grid.Row="1"
+                    Background="Transparent"
+                    BorderThickness="00"
+                    Click="Button_Sub_Click">
+                    <Path
+                        Data="M13.8535 5.85547L13.1445 5.14648L8 10.291L2.85547 5.14648L2.14648 5.85547L8 11.709L13.8535 5.85547Z"
+                        Fill="Black"
+                        Stretch="Uniform"
+                        Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:NumericUpDown}, Path=ShowBtn}" />
+                </Button>
+            </Grid>
         </Grid>
-    </Grid>
+    </Border>
 </UserControl>

+ 124 - 33
PDF Office/CustomControl/NumericUpDown.xaml.cs

@@ -21,24 +21,100 @@ namespace PDF_Office.CustomControl
     /// </summary>
     public partial class NumericUpDown : UserControl
     {
-        public int Num
+        
+
+        public string Text
         {
-            get
-            {
-                int value = 0;
-                this.Dispatcher.Invoke(new Action(() =>
-                    value = Convert.ToInt32(this.TextBox_Num.Text.Trim())
-                ));
-                return value;
-            }
+            get { return (string)GetValue(TextProperty); }
+            set { SetValue(TextProperty, value); }
+        }
+
+        public static readonly DependencyProperty TextProperty =
+            DependencyProperty.Register("Text", typeof(string), typeof(NumericUpDown), new PropertyMetadata(""));
+
+
+        /// <summary>
+        /// 当前值
+        /// </summary>
+        public double Value
+        {
+            get { return (double)GetValue(ValueProperty); }
             set
             {
-                this.Dispatcher.Invoke(new Action(() =>
+                if (value > Maximum)
+                {
+                    SetValue(ValueProperty, Maximum);
+                }
+                else if (value < Minimum)
+                {
+                    SetValue(ValueProperty, Minimum);
+                }
+                else
                 {
-                    this.TextBox_Num.Text = value.ToString();
-                }));
+                    SetValue(ValueProperty, value);
+                    TextBox_Num.Text = Value.ToString();
+                }
             }
         }
+
+        public static readonly DependencyProperty ValueProperty =
+            DependencyProperty.Register("Value", typeof(double), typeof(NumericUpDown), new PropertyMetadata(0.0));
+
+
+        /// <summary>
+        /// 最大值
+        /// </summary>
+        public double Maximum
+        {
+            get { return (double)GetValue(MaximumProperty); }
+            set { SetValue(MaximumProperty, value); }
+        }
+
+        public static readonly DependencyProperty MaximumProperty =
+            DependencyProperty.Register("Maximum", typeof(double), typeof(NumericUpDown), new PropertyMetadata(9999.0));
+
+
+
+        /// <summary>
+        /// 最小值
+        /// </summary>
+        public double Minimum
+        {
+            get { return (double)GetValue(MinimumProperty); }
+            set { SetValue(MinimumProperty, value); }
+        }
+
+        public static readonly DependencyProperty MinimumProperty =
+            DependencyProperty.Register("Minimum", typeof(double), typeof(NumericUpDown), new PropertyMetadata(0.0));
+
+
+        /// <summary>
+        /// 增长步长
+        /// </summary>
+
+        public double TickFrequency
+        {
+            get { return (double)GetValue(TickFrequencyProperty); }
+            set { SetValue(TickFrequencyProperty, value); }
+        }
+
+        public static readonly DependencyProperty TickFrequencyProperty =
+            DependencyProperty.Register("TickFrequency", typeof(double), typeof(NumericUpDown), new PropertyMetadata(1.0));
+
+
+
+        public Visibility ShowBtn
+        {
+            get { return (Visibility)GetValue(ShowBtnProperty); }
+            set { SetValue(ShowBtnProperty, value); }
+        }
+
+        public static readonly DependencyProperty ShowBtnProperty =
+            DependencyProperty.Register("ShowBtn", typeof(Visibility), typeof(NumericUpDown), new PropertyMetadata(Visibility.Visible));
+
+
+
+
         public NumericUpDown()
         {
             InitializeComponent();
@@ -46,43 +122,58 @@ namespace PDF_Office.CustomControl
 
         private void Button_Add_Click(object sender, RoutedEventArgs e)
         {
-            int num = int.Parse(this.TextBox_Num.Text.Trim());
-            if (num > 0)
+            int num = 0;
+            double targetvalue = 0;
+            bool result = int.TryParse(this.TextBox_Num.Text.Trim(), out num);
+            if (result)
+            {
+                targetvalue = num + TickFrequency;
+            }
+            else
             {
-                this.TextBox_Num.Text = (num + 1).ToString();
-                Text = this.TextBox_Num.Text;
+                targetvalue = Maximum;
             }
+            if (targetvalue > Maximum)
+            {
+                targetvalue = Maximum;
+            }
+            this.TextBox_Num.Text = targetvalue.ToString();
+            Text = this.TextBox_Num.Text;
+            Value = targetvalue;
         }
 
         private void Button_Sub_Click(object sender, RoutedEventArgs e)
         {
-            int num = int.Parse(this.TextBox_Num.Text.Trim());
-            if (num > 0)
+            int num = 0;
+            double targetvalue = 0;
+            bool result = int.TryParse(this.TextBox_Num.Text.Trim(), out num);
+            if (result)
+            {
+                targetvalue = num - TickFrequency;
+            }
+            else
             {
-                if ((num - 1) == 0)
-                    return;
-                this.TextBox_Num.Text = (num - 1).ToString();
-                Text = this.TextBox_Num.Text;
+                targetvalue = Minimum;
             }
-        } 
 
-        public string Text
-        {
-            get { return (string)GetValue(TextProperty); }
-            set { SetValue(TextProperty, value); }
+            if (targetvalue < Minimum)
+            {
+                targetvalue = Minimum;
+            }
+            this.TextBox_Num.Text = targetvalue.ToString();
+            Text = this.TextBox_Num.Text;
+            Value = targetvalue;
         }
 
-        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
-        public static readonly DependencyProperty TextProperty =
-            DependencyProperty.Register("Text", typeof(string), typeof(NumericUpDown), new PropertyMetadata(""));
-
         private void TextBox_Num_TextChanged(object sender, TextChangedEventArgs e)
         {
-            if (this.TextBox_Num.Text == "" || int.Parse(this.TextBox_Num.Text) < 1)
+            int num = 0;
+            if (this.TextBox_Num.Text == "" || !int.TryParse(this.TextBox_Num.Text, out num))
             {
-                this.TextBox_Num.Text = "1";
+                this.TextBox_Num.Text = Minimum.ToString();
             }
             Text = this.TextBox_Num.Text;
+            Value = int.Parse(this.TextBox_Num.Text);
         }
 
         private void CountTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)

+ 1 - 1
PDF Office/CustomControl/WritableComboBox.xaml

@@ -1,5 +1,5 @@
 <UserControl x:Class="PDF_Office.CustomControl.WritableComboBox"
-                   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

+ 7 - 0
PDF Office/PDF Office.csproj

@@ -189,6 +189,9 @@
     <Compile Include="CustomControl\AlertsMessage.xaml.cs">
       <DependentUpon>AlertsMessage.xaml</DependentUpon>
     </Compile>
+    <Compile Include="CustomControl\CommonWritableComboBox.xaml.cs">
+      <DependentUpon>CommonWritableComboBox.xaml</DependentUpon>
+    </Compile>
     <Compile Include="CustomControl\CustomIconToggleBtn.cs" />
     <Compile Include="CustomControl\DialogContent.cs" />
     <Compile Include="CustomControl\IconAndTextTabItem.cs" />
@@ -537,6 +540,10 @@
       <Generator>MSBuild:Compile</Generator>
       <SubType>Designer</SubType>
     </Page>
+    <Page Include="CustomControl\CommonWritableComboBox.xaml">
+      <SubType>Designer</SubType>
+      <Generator>MSBuild:Compile</Generator>
+    </Page>
     <Page Include="CustomControl\LoadingControl.xaml">
       <SubType>Designer</SubType>
       <Generator>MSBuild:Compile</Generator>

+ 1 - 0
PDF Office/Views/Dialog/HomePageToolsDialogs/HomePageInsertDialog.xaml

@@ -83,6 +83,7 @@
                     HorizontalAlignment="Right"
                     Command="{Binding InsertCommand}"
                     Content="插入" />
+            <cus:CommonWritableComboBox HorizontalAlignment="Left" Width="66" Height="32"></cus:CommonWritableComboBox>
         </Grid>
     </Grid>
 </UserControl>