Это работает в моем приложении
Просмотр:
<UserControl x:Class="PRWSolution.UI.View.MachineView"
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:PRWSolution.UI.View"
xmlns:language="clr-namespace:PRWSolution.UI.Properties.Langs"
xmlns:setting="clr-namespace:PRWSolution.UI.Properties"
mc:Ignorable="d"
DataContext="{Binding Machine, Source={StaticResource Locator}}"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:ie="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:cmd="http://www.galasoft.ch/mvvmlight"
TextElement.Foreground="{DynamicResource MaterialDesignBody}"
Background="{DynamicResource MaterialDesignPaper}"
TextElement.FontWeight="Medium"
TextElement.FontSize="14"
FontFamily="{materialDesign:MaterialDesignFont}">
<Grid>
<StackPanel Orientation="Vertical">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Grid.ColumnSpan="2">
<!--Toolbar for interaction-->
<ToolBarTray Grid.Column="0" Margin="0,5,0,0">
<ToolBar Style="{DynamicResource MaterialDesignToolBar}" ClipToBounds="False">
<Button x:Name="SaveMachine" ToolTip="Save" Command="{Binding SaveCommand, Mode=OneWay}">
<materialDesign:PackIcon Kind="ContentSave" />
</Button>
<Button x:Name="EditeMachine" ToolTip="Edit" Command="{Binding UpdateCommand, Mode=OneWay}">
<materialDesign:PackIcon Kind="Edit" />
</Button>
<Button x:Name="DeleteMachine" ToolTip="Delete" Command="{Binding DeleteCommand, Mode=OneWay}">
<materialDesign:PackIcon Kind="Delete" />
</Button>
<Separator />
<Button x:Name="ClearForm" ToolTip="Clear Form">
<materialDesign:PackIcon Kind="LayersClear" />
</Button>
</ToolBar>
</ToolBarTray>
<ToolBarTray Grid.Column="1" HorizontalAlignment="Right" Margin="0,5,0,0">
<ToolBar Style="{DynamicResource MaterialDesignToolBar}" ClipToBounds="False">
<Button x:Name="ExportMachine" ToolTip="Export" Click="ExportMachine_Click">
<materialDesign:PackIcon Kind="TableExport" />
</Button>
</ToolBar>
</ToolBarTray>
</Grid>
</Grid>
<!--Card for input database information-->
<materialDesign:Card Margin="5">
<StackPanel>
<!--Expander general machine information-->
<Expander Header="{x:Static language:Lang.ExpMachineData }" FontSize="11" HorizontalAlignment="Stretch" Margin="5,5,5,0">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<StackPanel Orientation="Vertical">
<TextBox x:Name="txtMachineID" Text="{Binding MachineID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" materialDesign:HintAssist.Hint="{x:Static language:Lang.DgMachineID}" materialDesign:TextFieldAssist.HasClearButton="True" Style="{StaticResource MaterialDesignFloatingHintTextBox}" VerticalAlignment="Center" Margin="5"/>
<TextBox x:Name="txtCustomerID" Text="{Binding CustomerID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" materialDesign:HintAssist.Hint="{x:Static language:Lang.DgCustomerId}" materialDesign:TextFieldAssist.HasClearButton="True" Style="{StaticResource MaterialDesignFloatingHintTextBox}" VerticalAlignment="Center" Margin="5"/>
</StackPanel>
</Grid>
<Grid Grid.Column="1">
<StackPanel Orientation="Vertical">
<ComboBox x:Name="cmbCustomerName" Width="300" HorizontalAlignment="Left" SelectedValuePath="ClientName" SelectedValue="{Binding CustomerName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding MachineCustomer.ClientDataContext, Source={StaticResource Locator}}" DisplayMemberPath="ClientName" materialDesign:HintAssist.Hint="Customer Name" Margin="5"/>
<TextBox x:Name="txtCity" Text="{Binding City, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" materialDesign:HintAssist.Hint="{x:Static language:Lang.DgCity}" materialDesign:TextFieldAssist.HasClearButton="True" Style="{StaticResource MaterialDesignFloatingHintTextBox}" VerticalAlignment="Center" Margin="5"/>
<TextBox x:Name="txtCountry" Text="{Binding Country, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" materialDesign:HintAssist.Hint="{x:Static language:Lang.DgCountry}" materialDesign:TextFieldAssist.HasClearButton="True" Style="{StaticResource MaterialDesignFloatingHintTextBox}" VerticalAlignment="Center" Margin="5"/>
</StackPanel>
</Grid>
</Grid>
</Expander>
<Border Background="{DynamicResource MaterialDesignDivider}" Height="1" HorizontalAlignment="Stretch" SnapsToDevicePixels="True" />
<!--Expander general part info-->
<Expander Header="Part Information" FontSize="11" HorizontalAlignment="Stretch" Margin="5,0,5,0">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<StackPanel Orientation="Vertical">
<TextBox x:Name="txtPartName" Text="{Binding PartName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" materialDesign:HintAssist.Hint="Part Name" materialDesign:TextFieldAssist.HasClearButton="True" Style="{StaticResource MaterialDesignFloatingHintTextBox}" VerticalAlignment="Center" Margin="5"/>
<TextBox x:Name="txtPartNumber" Text="{Binding PartNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" materialDesign:HintAssist.Hint="Part Number" materialDesign:TextFieldAssist.HasClearButton="True" Style="{StaticResource MaterialDesignFloatingHintTextBox}" VerticalAlignment="Center" Margin="5"/>
<TextBox x:Name="txtProject" Text="{Binding Project, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" materialDesign:HintAssist.Hint="Project" materialDesign:TextFieldAssist.HasClearButton="True" Style="{StaticResource MaterialDesignFloatingHintTextBox}" VerticalAlignment="Center" Margin="5"/>
</StackPanel>
</Grid>
</Grid>
</Expander>
<Border Background="{DynamicResource MaterialDesignDivider}" Height="1" HorizontalAlignment="Stretch" SnapsToDevicePixels="True" />
<!--Expander for Machine Serials-->
<Expander Header="{x:Static language:Lang.ExpMachineSerials}" FontSize="11" HorizontalAlignment="Stretch" Margin="5,0,5,0">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<StackPanel Orientation="Vertical">
<TextBox x:Name="txtSpindleC1" Text="{Binding SpindleC1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" materialDesign:HintAssist.Hint="{x:Static language:Lang.DgSpindleC1}" materialDesign:TextFieldAssist.HasClearButton="True" Style="{StaticResource MaterialDesignFloatingHintTextBox}" VerticalAlignment="Center" Margin="5"/>
<TextBox x:Name="txtSpindleC2" Text="{Binding SpindleC2, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" materialDesign:HintAssist.Hint="{x:Static language:Lang.DgSpindleC2}" materialDesign:TextFieldAssist.HasClearButton="True" Style="{StaticResource MaterialDesignFloatingHintTextBox}" VerticalAlignment="Center" Margin="5"/>
<TextBox x:Name="txtHoningHead" Text="{Binding HoningHead, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" materialDesign:HintAssist.Hint="{x:Static language:Lang.DgHoningHead}" materialDesign:TextFieldAssist.HasClearButton="True" Style="{StaticResource MaterialDesignFloatingHintTextBox}" VerticalAlignment="Center" Margin="5"/>
</StackPanel>
</Grid>
</Grid>
</Expander>
<Border Background="{DynamicResource MaterialDesignDivider}" Height="1" HorizontalAlignment="Stretch" SnapsToDevicePixels="True" />
<!--Expander for Softwareversion-->
<Expander Header="{x:Static language:Lang.ExpSoftwareVersion}" FontSize="11" HorizontalAlignment="Stretch" Margin="5,0,5,5">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<StackPanel Orientation="Vertical">
<TextBox x:Name="txtNCVersion" Text="{Binding NCVersion, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" materialDesign:HintAssist.Hint="{x:Static language:Lang.DgNCVersion}" materialDesign:TextFieldAssist.HasClearButton="True" Style="{StaticResource MaterialDesignFloatingHintTextBox}" VerticalAlignment="Center" Margin="5"/>
<TextBox x:Name="txtHMIVersion" Text="{Binding HMIVersion, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" materialDesign:HintAssist.Hint="{x:Static language:Lang.DgHMIVersion}" materialDesign:TextFieldAssist.HasClearButton="True" Style="{StaticResource MaterialDesignFloatingHintTextBox}" VerticalAlignment="Center" Margin="5"/>
<TextBox x:Name="txtHRIVersion" Text="{Binding HRIVersion, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" materialDesign:HintAssist.Hint="{x:Static language:Lang.DgHRIVersion}" materialDesign:TextFieldAssist.HasClearButton="True" Style="{StaticResource MaterialDesignFloatingHintTextBox}" VerticalAlignment="Center" Margin="5"/>
<TextBox x:Name="txtAHSVersion" Text="{Binding AHSVersion, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" materialDesign:HintAssist.Hint="{x:Static language:Lang.DgAHSVersion}" materialDesign:TextFieldAssist.HasClearButton="True" Style="{StaticResource MaterialDesignFloatingHintTextBox}" VerticalAlignment="Center" Margin="5"/>
</StackPanel>
</Grid>
</Grid>
</Expander>
</StackPanel>
</materialDesign:Card>
<!--Database datagrid-->
<DataGrid x:Name="MachineDataGrid" Margin="5" AutoGenerateColumns="False" MaxHeight="700" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" ItemsSource="{Binding DataContext, Mode=TwoWay}" SelectedItem="{Binding Path=MachineSelectedItem, Mode=TwoWay}">
<ie:Interaction.Triggers>
<ie:EventTrigger EventName="SelectionChanged">
<ie:InvokeCommandAction Command="{Binding SelectionChangedCommand}" CommandParameter="{Binding ElementName=MachineDataGrid, Path=SelectedItem}"/>
</ie:EventTrigger>
</ie:Interaction.Triggers>
<DataGrid.Columns>
<materialDesign:DataGridTextColumn Header="{x:Static language:Lang.DgMachineID}" Binding="{Binding MachineID, Mode=TwoWay}"/>
<materialDesign:DataGridTextColumn Header="{x:Static language:Lang.DgCustomerId}" Binding="{Binding CustomerID}"/>
<materialDesign:DataGridTextColumn Header="{x:Static language:Lang.DgCustomerName}" Binding="{Binding CustomerName}"/>
<materialDesign:DataGridTextColumn Header="{x:Static language:Lang.DgCity}" Binding="{Binding City}"/>
<materialDesign:DataGridTextColumn Header="{x:Static language:Lang.DgCountry}" Binding="{Binding Country}"/>
<materialDesign:DataGridTextColumn Header="Part Name" Binding="{Binding PartName}"/>
<materialDesign:DataGridTextColumn Header="Part Number" Binding="{Binding PartNumber}"/>
<materialDesign:DataGridTextColumn Header="Project" Binding="{Binding Project}"/>
<materialDesign:DataGridTextColumn Header="{x:Static language:Lang.DgSpindleC1}" Binding="{Binding SpindleC1}"/>
<materialDesign:DataGridTextColumn Header="{x:Static language:Lang.DgSpindleC2}" Binding="{Binding SpindleC2}"/>
<materialDesign:DataGridTextColumn Header="{x:Static language:Lang.DgHoningHead}" Binding="{Binding HoningHead}"/>
<materialDesign:DataGridTextColumn Header="{x:Static language:Lang.DgNCVersion}" Binding="{Binding NCVersion}"/>
<materialDesign:DataGridTextColumn Header="{x:Static language:Lang.DgHMIVersion}" Binding="{Binding HMIVersion}"/>
<materialDesign:DataGridTextColumn Header="{x:Static language:Lang.DgHRIVersion}" Binding="{Binding HRIVersion}"/>
<materialDesign:DataGridTextColumn Header="{x:Static language:Lang.DgAHSVersion}" Binding="{Binding AHSVersion}"/>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</Grid>
ViewModel:
public class MachineViewModel : ViewModelBase
{
#region public statments Textbox text
//Public statments to get textbox text
public string MachineID { get; set; }
public string CustomerID { get; set; }
public string CustomerName { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string PartName { get; set; }
public string PartNumber { get; set; }
public string Project { get; set; }
public string SpindleC1 { get; set; }
public string SpindleC2 { get; set; }
public string HoningHead { get; set; }
public string NCVersion { get; set; }
public string HMIVersion { get; set; }
public string HRIVersion { get; set; }
public string AHSVersion { get; set; }
#endregion
//DialogService
private IDialogService _dialogService;
//Operation button for save, update, delete, selection changed and print
public ICommand SaveCommand { get; private set; }
public ICommand UpdateCommand { get; private set; }
public ICommand DeleteCommand { get; private set; }
public ICommand ClearCommand { get; private set; }
public ICommand SelectionChangedCommand { get; set; }
//observable collection for machine model
public ObservableCollection<Machine> DataContext { get; set; }
private Machine machineSelectedItem;
public Machine MachineSelectedItem
{
get { return machineSelectedItem; }
set { machineSelectedItem = value; }
}
public object MachineDataGrid { get; set; }
public List<MachineClient> cmbclientDetails;
//PRWContext for general use
private PRWContext context = new PRWContext();
public MachineViewModel()
{
//Commands for save, update, delete and print
SaveCommand = new RelayCommand(() => ExecuteSaveCommand());
UpdateCommand = new RelayCommand(() => ExecuteUpdateCommand());
DeleteCommand = new RelayCommand(() => ExecuteDeleteCommand());
ClearCommand = new RelayCommand(() => ExecuteClearCommand());
SelectionChangedCommand = new RelayCommand(() => ExecuteSelectionChangedCommand());
//Load the data from PRW Database to datagrid
LoadData();
//Normelly done with dependency injection
_dialogService = new DialogService();
}
//execute save
private void ExecuteSaveCommand()
{
Machine machine = new Machine
{
//Machine data
MachineID = MachineID,
CustomerID = CustomerID,
CustomerName = CustomerName,
City = City,
Country = Country,
//Part data
PartName = PartName,
PartNumber = PartNumber,
Project = Project,
//Serial data
SpindleC1 = SpindleC1,
SpindleC2 = SpindleC2,
HoningHead = HoningHead,
//Softwareversion data
NCVersion = NCVersion,
HMIVersion = HMIVersion,
HRIVersion = HRIVersion,
AHSVersion = AHSVersion
};
context.Machines.Add(machine);
context.SaveChanges();
ClearContent();
}
//execute update
private void ExecuteUpdateCommand()
{
Machine machine = context.Machines.FirstOrDefault(w => w.MachineID == MachineID);
machine.CustomerID = CustomerID;
machine.CustomerName = CustomerName;
machine.City = City;
machine.Country = Country;
machine.PartName = PartName;
machine.PartNumber = PartNumber;
machine.Project = Project;
machine.SpindleC1 = SpindleC1;
machine.SpindleC2 = SpindleC2;
machine.HoningHead = HoningHead;
machine.NCVersion = NCVersion;
machine.HMIVersion = HMIVersion;
machine.HRIVersion = HRIVersion;
machine.AHSVersion = AHSVersion;
context.Machines.AddOrUpdate(machine);
context.SaveChanges();
ClearContent();
}
//execute delete
private void ExecuteDeleteCommand()
{
if (machineSelectedItem != null)
{
var dialog = new YesNoDialogViewModel("Question", "Your are Sure to Delete the Record?");
var result = _dialogService.OpenDialog(dialog);
if (result == DialogResults.Yes)
{
Machine machine = context.Machines.FirstOrDefault(w => w.MachineID == MachineID);
context.Machines.Remove(machine);
context.SaveChanges();
ClearContent();
}
}
else
{
var dialog = new AlertDialogViewModel("Attention", "Please select a Record!");
var result = _dialogService.OpenDialog(dialog);
Console.WriteLine(result);
}
}
//execute clear
private void ExecuteClearCommand()
{
ClearContent();
}
// Execute selection changed
private void ExecuteSelectionChangedCommand()
{
if(machineSelectedItem != null)
{
MachineID = machineSelectedItem.MachineID?.ToString() ?? "";
CustomerID = machineSelectedItem.CustomerID?.ToString() ?? "";
CustomerName = machineSelectedItem.CustomerName?.ToString() ?? "";
City = machineSelectedItem.City?.ToString() ?? "";
Country = machineSelectedItem.Country?.ToString() ?? "";
PartName = machineSelectedItem.PartName?.ToString() ?? "";
PartNumber = machineSelectedItem.PartNumber?.ToString() ?? "";
Project = machineSelectedItem.Project?.ToString() ?? "";
SpindleC1 = machineSelectedItem.SpindleC1?.ToString() ?? "";
SpindleC2 = machineSelectedItem.SpindleC2?.ToString() ?? "";
HoningHead = machineSelectedItem.HoningHead?.ToString() ?? "";
NCVersion = machineSelectedItem.NCVersion?.ToString() ?? "";
HMIVersion = machineSelectedItem.HMIVersion?.ToString() ?? "";
HRIVersion = machineSelectedItem.HRIVersion?.ToString() ?? "";
AHSVersion = machineSelectedItem.AHSVersion?.ToString() ?? "";
}
else
{
machineSelectedItem = null;
}
}
//Load data from database to grid
private void LoadData()
{
context.Machines.Load();
this.DataContext = context.Machines.Local;
}
//Clear textboxes
private void ClearContent()
{
MachineID = string.Empty;
CustomerID = string.Empty;
CustomerName = string.Empty;
City = string.Empty;
PartName = string.Empty;
PartNumber = string.Empty;
Project = string.Empty;
Country = string.Empty;
SpindleC1 = string.Empty;
SpindleC2 = string.Empty;
HoningHead = string.Empty;
NCVersion = string.Empty;
HMIVersion = string.Empty;
HRIVersion = string.Empty;
AHSVersion = string.Empty;
}
}