diff --git a/DyeingComputer.csproj b/DyeingComputer.csproj index df00ac5..2d96412 100644 --- a/DyeingComputer.csproj +++ b/DyeingComputer.csproj @@ -91,6 +91,7 @@ + diff --git a/UserClass/NumericTextColumn .cs b/UserClass/NumericTextColumn .cs new file mode 100644 index 0000000..16df20e --- /dev/null +++ b/UserClass/NumericTextColumn .cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Controls; +using System.Windows.Input; +using System.Windows; + +namespace DyeingComputer.UserClass +{ + /// + /// DataGrid中只能输入数字的列 + /// + /// local:DataGridNumericColumn Binding = "{Binding NumericProperty}" + /// + public class NumericTextColumn : DataGridTextColumn + { + protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs) + { + var edit = editingElement as TextBox; + edit.PreviewTextInput += Edit_PreviewTextInput; + DataObject.AddPastingHandler(edit, OnPaste); + //限制输入法切换,可避免中文输入添加到列中 + InputMethod.SetIsInputMethodEnabled(edit, false); + return base.PrepareCellForEdit(editingElement, editingEventArgs); + } + + private void OnPaste(object sender, DataObjectPastingEventArgs e) + { + var data = e.SourceDataObject.GetData(DataFormats.Text); + if (!IsDataValid(data)) e.CancelCommand(); + } + + private void Edit_PreviewTextInput(object sender, TextCompositionEventArgs e) + { + e.Handled = !IsDataValid(e.Text); + } + + bool IsDataValid(object data) + { + try + { + Convert.ToInt32(data); + return true; + } + catch + { + return false; + } + } + } +} diff --git a/View/ParameterSetView.xaml b/View/ParameterSetView.xaml index d3c6f01..da2ed2f 100644 --- a/View/ParameterSetView.xaml +++ b/View/ParameterSetView.xaml @@ -5,16 +5,17 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:DyeingComputer.View" xmlns:lang="clr-namespace:DyeingComputer.Properties" + xmlns:localUserClass="clr-namespace:DyeingComputer.UserClass" mc:Ignorable="d" d:DesignHeight="630" d:DesignWidth="1280" VerticalAlignment="Top"> - + CanUserAddRows="False" CanUserDeleteRows="False" HeadersVisibility ="Column" InputMethod.IsInputMethodEnabled ="False" + RowEditEnding="Grid_RowEditEnding" + Background="White" SelectionMode="Single" FontSize="15" Cursor="AppStarting" > - - + diff --git a/View/ParameterSetView.xaml.cs b/View/ParameterSetView.xaml.cs index ecdb6a3..4ab0449 100644 --- a/View/ParameterSetView.xaml.cs +++ b/View/ParameterSetView.xaml.cs @@ -1,17 +1,23 @@ using System; using System.Collections.Generic; +using System.Data; +using System.Data.Entity.Core.Common.CommandTrees.ExpressionBuilder; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; +using System.Windows.Controls.Primitives; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; +using System.Windows.Media.Animation; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; +using static DyeingComputer.UserClass.SqliteHelper; namespace DyeingComputer.View { @@ -23,6 +29,67 @@ namespace DyeingComputer.View public ParameterSetView() { InitializeComponent(); + + set_sql(); + // Grid.CurrentCell = new DataGridCellInfo(Grid.Items[1], Grid.Columns[3]); + + FocusManager.SetFocusedElement(Grid, Grid); + Grid.CurrentCell = new DataGridCellInfo(Grid.Items[Grid.Items.Count-1], Grid.Columns[3]); + Grid.BeginEdit(); + //FocusManager.SetFocusedElement(Grid, Grid); + + + + } + + + + + private SQLiteHelper SQLiteHelpers = null; //定义数据库 + private readonly string DBAddress = Environment.CurrentDirectory + "\\DataBase\\800COMPUTER.db"; //数据库路径 + DataSet sql; //内存数据缓存 + + public void set_sql() + { + SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径 + SQLiteHelpers.Open(); //打开数据库 + + string sql_script = "select * from Parameters"; + + if (sql != null) sql.Clear(); //清空缓存 + sql = SQLiteHelpers.ExecuteDataSet(sql_script, null); //读取计划表写入缓存 + if (sql != null) Grid.ItemsSource = sql.Tables[0].DefaultView; //转换显示计划表 + + SQLiteHelpers.Close(); //关闭连接 + + //sql.Clear(); //清除缓存 + //System.GC.Collect(); + } + + private void Grid_SelectionChanged(object sender, SelectionChangedEventArgs e)//表格选择事件 + { + string ID; + string Numder; + + int rownum = Grid.SelectedIndex;//获取鼠标选中行并定义变量 + if (rownum != -1)//判断鼠标定位是否有效 + { + ID = (Grid.Columns[1].GetCellContent(Grid.Items[rownum]) as TextBlock).Text;//定位第0列, + Numder = (Grid.Columns[3].GetCellContent(Grid.Items[rownum]) as TextBlock).Text;//定位第1列, + } + } + + private void Grid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e) + { + string ID; + string Numder; + + int rownum = Grid.SelectedIndex;//获取鼠标选中行并定义变量 + if (rownum != -1)//判断鼠标定位是否有效 + { + ID = (Grid.Columns[1].GetCellContent(Grid.Items[rownum]) as TextBlock).Text;//定位第0列, + Numder = (Grid.Columns[3].GetCellContent(Grid.Items[rownum]) as TextBlock).Text;//定位第1列, + } } } }