sc 2 years ago
parent
commit
020fdc37c8
  1. 32
      MainWindow.xaml
  2. 37
      MainWindow.xaml.cs
  3. 63
      UserClass/DataTableForToObservableCollection.cs
  4. 176
      ViewModel/MainWindowViewModel.cs
  5. 1
      formula_manage.csproj

32
MainWindow.xaml

@ -10,7 +10,7 @@
DataContext="{Binding Source={StaticResource Locator}, Path=Main}" DataContext="{Binding Source={StaticResource Locator}, Path=Main}"
BorderBrush="White" Background="#FFE0E0E0"> BorderBrush="White" Background="#FFE0E0E0">
<Window.Resources> <Window.Resources>
<ViewModel:MainWindowViewModel x:Key="mvm"/> <ViewModel:MainWindowViewModel x:Key="MainWindowViewModel"/>
</Window.Resources> </Window.Resources>
<Grid> <Grid>
<Menu IsTabStop="False"> <Menu IsTabStop="False">
@ -71,7 +71,8 @@
<TextBox x:Name="Anew" HorizontalAlignment="Left" Height="30" Margin="300,35,0,0" TextWrapping="Wrap" Text="0" VerticalAlignment="Top" Width="30" FontSize="20" <TextBox x:Name="Anew" HorizontalAlignment="Left" Height="30" Margin="300,35,0,0" TextWrapping="Wrap" Text="0" VerticalAlignment="Top" Width="30" FontSize="20"
MaxLines="1" MaxLength="2" PreviewTextInput="Tb_KeyPress" InputMethod.IsInputMethodEnabled="False" Focusable ="False"/> MaxLines="1" MaxLength="2" PreviewTextInput="Tb_KeyPress" InputMethod.IsInputMethodEnabled="False" Focusable ="False"/>
<TextBox HorizontalAlignment="Left" Height="30" Margin="95,105,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="200" FontSize="20" KeyUp="SearchBox_OnKeyDownd" IsTabStop="False"/> <TextBox HorizontalAlignment="Left" Height="30" Margin="95,105,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="200" FontSize="20" KeyUp="SearchBox_OnKeyDownd" IsTabStop="False"/>
<ComboBox x:Name="Machine" DisplayMemberPath="Name" HorizontalAlignment="Left" Height="30" Margin="95,140,0,0" VerticalAlignment="Top" Width="120" FontSize="20" KeyUp="SearchBox_OnKeyDownd" IsEditable="True"/> <ComboBox x:Name="Machine" DisplayMemberPath="Name" ItemsSource="{Binding mac_Machine, Source={StaticResource MainWindowViewModel}}"
HorizontalAlignment="Left" Height="30" Margin="95,140,0,0" VerticalAlignment="Top" Width="120" FontSize="20" KeyUp="SearchBox_OnKeyDownd" IsEditable="True"/>
<ComboBox x:Name="OrderType" Text="正常单" HorizontalAlignment="Left" Height="30" Margin="95,175,0,0" VerticalAlignment="Top" Width="120" FontSize="20" IsTabStop="True" KeyUp="SearchBox_OnKeyDownd" IsEditable="True"> <ComboBox x:Name="OrderType" Text="正常单" HorizontalAlignment="Left" Height="30" Margin="95,175,0,0" VerticalAlignment="Top" Width="120" FontSize="20" IsTabStop="True" KeyUp="SearchBox_OnKeyDownd" IsEditable="True">
<ComboBoxItem Content="正常单"></ComboBoxItem> <ComboBoxItem Content="正常单"></ComboBoxItem>
<ComboBoxItem Content="追加单"></ComboBoxItem> <ComboBoxItem Content="追加单"></ComboBoxItem>
@ -97,7 +98,8 @@
<Button Content="-" HorizontalAlignment="Left" Height="15" Width="20" Margin="140,20,0,0" VerticalAlignment="Top" FontSize="10" <Button Content="-" HorizontalAlignment="Left" Height="15" Width="20" Margin="140,20,0,0" VerticalAlignment="Top" FontSize="10"
Background="#FFF1F1F1" BorderBrush="#FFC5C5C5" Padding="0,0,0,0" IsTabStop="False" Click="Button_StepDow" Focusable ="False"/> Background="#FFF1F1F1" BorderBrush="#FFC5C5C5" Padding="0,0,0,0" IsTabStop="False" Click="Button_StepDow" Focusable ="False"/>
<TextBlock HorizontalAlignment="Left" Height="35" Margin="170,5,0,0" TextWrapping="Wrap" Text="流程:" VerticalAlignment="Top" Width="45" FontSize="20"/> <TextBlock HorizontalAlignment="Left" Height="35" Margin="170,5,0,0" TextWrapping="Wrap" Text="流程:" VerticalAlignment="Top" Width="45" FontSize="20"/>
<ComboBox Name="Workflow" DisplayMemberPath="DissolveName" HorizontalAlignment="Left" Height="30" Margin="225,5,0,0" Text="" VerticalAlignment="Top" Width="175" FontSize="20" IsEditable="True" Grid.ColumnSpan="2"/> <ComboBox Name="Workflow" DisplayMemberPath="DissolveName" ItemsSource="{Binding flow_Workflow, Source={StaticResource MainWindowViewModel}}"
HorizontalAlignment="Left" Height="30" Margin="225,5,0,0" Text="" VerticalAlignment="Top" Width="175" FontSize="20" IsEditable="True" Grid.ColumnSpan="2"/>
<TextBlock HorizontalAlignment="Left" Height="35" Margin="0,40,0,0" TextWrapping="Wrap" Text="配方代码:" VerticalAlignment="Top" Width="90" FontSize="20"/> <TextBlock HorizontalAlignment="Left" Height="35" Margin="0,40,0,0" TextWrapping="Wrap" Text="配方代码:" VerticalAlignment="Top" Width="90" FontSize="20"/>
<TextBox HorizontalAlignment="Left" Height="30" Margin="90,40,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="125" FontSize="20" KeyUp="SearchBox_OnKeyDownd"/> <TextBox HorizontalAlignment="Left" Height="30" Margin="90,40,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="125" FontSize="20" KeyUp="SearchBox_OnKeyDownd"/>
<TextBox HorizontalAlignment="Left" Height="30" Margin="225,40,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="140" FontSize="20" Focusable ="False" Grid.ColumnSpan="2"/> <TextBox HorizontalAlignment="Left" Height="30" Margin="225,40,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="140" FontSize="20" Focusable ="False" Grid.ColumnSpan="2"/>
@ -163,17 +165,12 @@
<DataGridTemplateColumn Header="原料代码" Width="200" MaxWidth="400" MinWidth="100" IsReadOnly="False"> <DataGridTemplateColumn Header="原料代码" Width="200" MaxWidth="400" MinWidth="100" IsReadOnly="False">
<DataGridTemplateColumn.CellTemplate> <DataGridTemplateColumn.CellTemplate>
<DataTemplate> <DataTemplate>
<ComboBox x:Name="CP_PRODUCT_CODE" BorderThickness="0,0,0,0" <ComboBox x:Name="CP_PRODUCT_CODE" BorderThickness="0,0,0,0"
ItemsSource="{Binding stuff_Product, Source={StaticResource MainWindowViewModel}}"
ItemsSource="{Binding DataContext.STUFFdataTable , RelativeSource={RelativeSource AncestorType={x:Type Window}}}" DisplayMemberPath="ProductCode"
DisplayMemberPath="ProductCode"
Text="{Binding PRODUCT_CODE}" Text="{Binding PRODUCT_CODE}"
BorderBrush="{x:Null}" Background="{x:Null}" IsEditable="True" KeyDown="CP_PRODUCT_CODE_KeyUp" BorderBrush="{x:Null}" Background="{x:Null}" IsEditable="True" KeyDown="CP_PRODUCT_CODE_KeyUp" >
>
</ComboBox> </ComboBox>
</DataTemplate> </DataTemplate>
</DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn.CellTemplate>
@ -198,10 +195,11 @@
<DataGridTemplateColumn Header="原料名称" Width="200" MaxWidth="400" MinWidth="100" CellStyle="{StaticResource CellStyle}"> <DataGridTemplateColumn Header="原料名称" Width="200" MaxWidth="400" MinWidth="100" CellStyle="{StaticResource CellStyle}">
<DataGridTemplateColumn.CellTemplate> <DataGridTemplateColumn.CellTemplate>
<DataTemplate> <DataTemplate>
<ComboBox x:Name="CP_PRODUCT_NAME" BorderThickness="0,0,0,0" <ComboBox x:Name="CP_PRODUCT_NAME" BorderThickness="0,0,0,0"
ItemsSource="{Binding STUFFdataTable }" ItemsSource="{Binding stuff_Product, Source={StaticResource MainWindowViewModel}}"
DisplayMemberPath="ProductName" DisplayMemberPath="ProductName"
Text="{Binding PRODUCT_NAME}" BorderBrush="{x:Null}" Background="{x:Null}" IsEditable="True" IsReadOnly="True" Focusable="True" IsTabStop="False"/> Text="{Binding PRODUCT_NAME}"
BorderBrush="{x:Null}" Background="{x:Null}" IsEditable="True" IsReadOnly="True" Focusable="True" IsTabStop="False"/>
</DataTemplate> </DataTemplate>
</DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn> </DataGridTemplateColumn>

37
MainWindow.xaml.cs

@ -104,7 +104,7 @@ namespace formula_manage
_statistics.IsEnabled = true; _statistics.IsEnabled = true;
} }
sql_();//查询stuff表 sql_();
Permissions_(); //权限管理 Permissions_(); //权限管理
CountDown();//执行循环方法 CountDown();//执行循环方法
@ -122,11 +122,6 @@ namespace formula_manage
} }
Grid_RRODUCT.ItemsSource = RRODUCTdataTable.DefaultView; Grid_RRODUCT.ItemsSource = RRODUCTdataTable.DefaultView;
//this.DataContext = Grid_RRODUCT;
// this.DataContext = STUFFdataTable.DefaultView;
Grid_RRODUCT.DataContext = this;
} }
private async void sql_() private async void sql_()
@ -144,35 +139,7 @@ namespace formula_manage
else else
{ {
Connstr_SC = "server=" + TEXT_SQLIP + ";database=" + TEXT_SQLNAME + ";User ID=" + TEXT_SQLUSER + ";Password=" + TEXT_SQLPASWOR; Connstr_SC = "server=" + TEXT_SQLIP + ";database=" + TEXT_SQLNAME + ";User ID=" + TEXT_SQLUSER + ";Password=" + TEXT_SQLPASWOR;
} }
string Stuff_sql = "SELECT ProductCode ,ProductName ,ProductType ,Concentration FROM [Dispensing].[dbo].[PRODUCT] order by ProductCode asc";//查询STUFF语句
string MAC_sql = "SELECT Name ,MacGroup ,Capacity ,Volume ,Industry ,Categories ,Note FROM [Dispensing].[dbo].[MACHINE] order by Name asc";//查询machine语句
string Dissolve_sql = "SELECT DissolveCode ,DissolveName ,MaterialType ,WeightMIN ,WeightMAX REMARK FROM [Dispensing].[dbo].[Dissolve]";//查询语句
SqlConnection conn_SC = new SqlConnection(Connstr_SC); //实例化
try
{
await conn_SC.OpenAsync(); //打开数据连接
SqlDataAdapter Stuff_data = new SqlDataAdapter(Stuff_sql, Connstr_SC); //查询stuff
SqlDataAdapter Mac_data = new SqlDataAdapter(MAC_sql, Connstr_SC); //查询machine
SqlDataAdapter Dissolve_data = new SqlDataAdapter(Dissolve_sql, Connstr_SC); //查询Dissolve
Stuff_data.Fill(STUFFdataTable); //stuff查询结果存入缓存
Mac_data.Fill(MACHINEdataTable); //machine查询结果存入缓存
Dissolve_data.Fill(DissolvedataTable); //machine查询结果存入缓存
conn_SC.Close(); //关闭连接
//DataGridStuff.ItemsSource = dataTable.DefaultView; //数据加入表格
}
catch (Exception)
{
System.Windows.MessageBox.Show("请求原料信息失败,检查连接");
return;
}
Machine.ItemsSource = MACHINEdataTable.DefaultView; // MACHINEdataTable数据集传递到ComboBox:machine
Workflow.ItemsSource = DissolvedataTable.DefaultView; // DissolvedataTable数据集传递到ComboBox:Workflow
} }
private async void Permissions_() private async void Permissions_()

63
UserClass/DataTableForToObservableCollection.cs

@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace formula_manage.UserClass
{
internal class DataTableToObservableCollection
{
public ObservableCollection<T> ToObservableCollection<T>(DataTable dt) where T : class, new()
{
Type t = typeof(T);
PropertyInfo[] propertys = t.GetProperties();
ObservableCollection<T> lst = new ObservableCollection<T>();
string typeName = string.Empty;
foreach (DataRow dr in dt.Rows)
{
T entity = new T();
foreach (PropertyInfo pi in propertys)
{
typeName = pi.Name;
if (dt.Columns.Contains(typeName))
{
if (!pi.CanWrite) continue;
object value = dr[typeName];
if (value == DBNull.Value) continue;
if (pi.PropertyType == typeof(string))
{
pi.SetValue(entity, value.ToString(), null);
}
else if (pi.PropertyType == typeof(int) || pi.PropertyType == typeof(int?))
{
pi.SetValue(entity, int.Parse(value.ToString()), null);
}
else if (pi.PropertyType == typeof(DateTime?) || pi.PropertyType == typeof(DateTime))
{
pi.SetValue(entity, DateTime.Parse(value.ToString()), null);
}
else if (pi.PropertyType == typeof(float))
{
pi.SetValue(entity, float.Parse(value.ToString()), null);
}
else if (pi.PropertyType == typeof(double))
{
pi.SetValue(entity, double.Parse(value.ToString()), null);
}
else
{
pi.SetValue(entity, value, null);
}
}
}
lst.Add(entity);
}
return lst;
}
}
}

176
ViewModel/MainWindowViewModel.cs

@ -58,7 +58,7 @@ public class MainWindowViewModel : ViewModelBase
} }
string Stuff_sql = "SELECT ProductCode ,ProductName ,ProductType ,Concentration FROM [Dispensing].[dbo].[PRODUCT] order by ProductCode asc";//查询STUFF语句 string Stuff_sql = "SELECT ProductCode ,ProductName ,ProductType ,Concentration FROM [Dispensing].[dbo].[PRODUCT] order by ProductCode asc";//查询STUFF语句
string MAC_sql = "SELECT Name ,MacGroup ,Capacity ,Volume ,Industry ,Categories ,Note FROM [Dispensing].[dbo].[MACHINE] order by Name asc";//查询machine语句 string MAC_sql = "SELECT Name ,Capacity ,Volume FROM [Dispensing].[dbo].[MACHINE] order by Name asc";//查询machine语句
string Dissolve_sql = "SELECT DissolveCode ,DissolveName ,MaterialType ,WeightMIN ,WeightMAX REMARK FROM [Dispensing].[dbo].[Dissolve]";//查询语句 string Dissolve_sql = "SELECT DissolveCode ,DissolveName ,MaterialType ,WeightMIN ,WeightMAX REMARK FROM [Dispensing].[dbo].[Dissolve]";//查询语句
SqlConnection conn_SC = new SqlConnection(Connstr_SC); //实例化 SqlConnection conn_SC = new SqlConnection(Connstr_SC); //实例化
@ -71,96 +71,82 @@ public class MainWindowViewModel : ViewModelBase
Stuff_data.Fill(STUFFdataTable); //stuff查询结果存入缓存 Stuff_data.Fill(STUFFdataTable); //stuff查询结果存入缓存
Mac_data.Fill(MACHINEdataTable); //machine查询结果存入缓存 Mac_data.Fill(MACHINEdataTable); //machine查询结果存入缓存
Dissolve_data.Fill(DissolvedataTable); //machine查询结果存入缓存 Dissolve_data.Fill(DissolvedataTable); //Dissolve_data查询结果存入缓存
conn_SC.Close(); //关闭连接 conn_SC.Close(); //关闭连接
} }
catch (Exception) catch (Exception)
{ {
System.Windows.MessageBox.Show("请求原料信息失败,检查连接"); System.Windows.MessageBox.Show("请求原料信息失败,检查连接");
return; return;
} }
// IEnumerable STUFFdata = STUFFdataTable.DefaultView;
// Machine.ItemsSource = MACHINEdataTable.DefaultView; // MACHINEdataTable数据集传递到ComboBox:machine
// Workflow.ItemsSource = DissolvedataTable.DefaultView; // DissolvedataTable数据集传递到ComboBox:Workflow
} }
public MainWindowViewModel() public MainWindowViewModel()
{ {
sql_(); sql_();
stuff_Product = ToObservableCollection<Product>(STUFFdataTable); //stuff_Product表转换
mac_Machine = ToObservableCollection<Machine>(MACHINEdataTable);
flow_Workflow = ToObservableCollection<Workflow>(DissolvedataTable);
}
// public ObservableCollection (System.Collections.Generic.List<T> list);
// BarCollection = (ObservableCollection<BarModel>)STUFFdataTable.AsDataView();
// var qwe = STUFFdataTable.DefaultView;
// var product = STUFFdataTable.DefaultView;
// BarCollection = (ObservableCollection<Product>)STUFFdata;
/* BarCollection = new ObservableCollection<Product>
{
new Product { ProductCode = "Bar 10", },
new Product { ProductCode = "Bar 20", },
new Product { ProductCode = "Bar 30", },
};*/
/* FooCollection = new ObservableCollection<FooViewModel>
{
new FooViewModel{ Id = 1, },
new FooViewModel{ Id = 2, },
new FooViewModel{ Id = 3, },
};*/
/* product = new ObservableCollection<Product>; public ObservableCollection<Product> stuff_Product { get; set; } //stuff_Product动态表实力化
foreach (DataRow row in STUFFdataTable.Rows) public ObservableCollection<Machine> mac_Machine { get; set; } //mac_Machine动态表实力化
public ObservableCollection<Workflow> flow_Workflow { get; set; } //Dissolve动态表实力化
public ObservableCollection<T> ToObservableCollection<T>(DataTable dt) where T : class, new()
{
Type t = typeof(T);
PropertyInfo[] propertys = t.GetProperties();
ObservableCollection<T> lst = new ObservableCollection<T>();
string typeName = string.Empty;
foreach (DataRow dr in dt.Rows)
{
T entity = new T();
foreach (PropertyInfo pi in propertys)
{ {
product.Add(new Product((int)row[0], (string)row[1], (string)row[2])); typeName = pi.Name;
if (dt.Columns.Contains(typeName))
} */ {
if (!pi.CanWrite) continue;
// _product = new ObservableCollection<Product>((IEnumerable<Product>)); object value = dr[typeName];
// { if (value == DBNull.Value) continue;
// new Product{ ProductCode= STUFFdataTable.Rows,ProductName= "123", ProductType= 12,Concentration= 12} if (pi.PropertyType == typeof(string))
// }; {
pi.SetValue(entity, value.ToString(), null);
}
else if (pi.PropertyType == typeof(int) || pi.PropertyType == typeof(int?))
{
pi.SetValue(entity, int.Parse(value.ToString()), null);
}
else if (pi.PropertyType == typeof(DateTime?) || pi.PropertyType == typeof(DateTime))
{
pi.SetValue(entity, DateTime.Parse(value.ToString()), null);
}
else if (pi.PropertyType == typeof(float))
{
pi.SetValue(entity, float.Parse(value.ToString()), null);
}
else if (pi.PropertyType == typeof(double))
{
pi.SetValue(entity, double.Parse(value.ToString()), null);
}
else
{
pi.SetValue(entity, value, null);
}
}
}
lst.Add(entity);
}
return lst;
} }
// private ObservableCollection<string> _selectionList = new ObservableCollection<string>(); //这个是实现的重要一步
public ObservableCollection<Product> _product { get; set; }
// public ObservableCollection<FooViewModel> FooCollection { get; set; }
} }
/* public class FooViewModel : ViewModelBase public class Product //stuff_Product
{
private BarModel _bar;
public int Id { get; set; }
public BarModel Bar { get => _bar; set => Set(ref _bar, value); }
}*/
public class Product
{ {
public string ProductCode { get; set; } public string ProductCode { get; set; }
public string ProductName { get; set; } public string ProductName { get; set; }
@ -172,25 +158,47 @@ public class MainWindowViewModel : ViewModelBase
return ProductCode; return ProductCode;
} }
} }
public class Machine //mac
{
public string Name { get; set; }
public float Volume { get; set; }
public float Capacity { get; set; }
/// <summary> public override string ToString()
/// StuffViewModeldl
/// </summary>
/* public RRODUCTiewModel()
{ {
//products = new ProductProvider().Select(); return Name;
DispatcherTimer timer = new DispatcherTimer//初始化循环,每0.5秒调用一次Tick_Event
{
Interval = TimeSpan.FromSeconds(0.1)
};
timer.Tick += Tick_Event;
timer.Start();
} }
}
public class Workflow //Dissolve
{
public string DissolveName { get; set; }
public int MaterialType { get; set; }
public int WeightMIN { get; set; }
public int WeightMAX { get; set; }
void Tick_Event(object sender, EventArgs e)//Tick_Event周期执行事件 public override string ToString()
{ {
return DissolveName;
}
}
/// <summary>
/// StuffViewModeldl
/// </summary>
/* public RRODUCTiewModel()
{
//products = new ProductProvider().Select();
DispatcherTimer timer = new DispatcherTimer//初始化循环,每0.5秒调用一次Tick_Event
{
Interval = TimeSpan.FromSeconds(0.1)
};
timer.Tick += Tick_Event;
timer.Start();
}
void Tick_Event(object sender, EventArgs e)//Tick_Event周期执行事件
{
}*/
}*/
} }

1
formula_manage.csproj

@ -178,6 +178,7 @@
<Compile Include="SQLModels\STUFF_Product.cs" /> <Compile Include="SQLModels\STUFF_Product.cs" />
<Compile Include="UserClass\IniFile.cs" /> <Compile Include="UserClass\IniFile.cs" />
<Compile Include="UserClass\PressKey.cs" /> <Compile Include="UserClass\PressKey.cs" />
<Compile Include="UserClass\DataTableForToObservableCollection.cs" />
<Compile Include="ViewModel\MainWindowViewModel.cs" /> <Compile Include="ViewModel\MainWindowViewModel.cs" />
<Compile Include="ViewModel\ViewModelLocator.cs" /> <Compile Include="ViewModel\ViewModelLocator.cs" />
<Compile Include="View\Formula.xaml.cs"> <Compile Include="View\Formula.xaml.cs">

Loading…
Cancel
Save