我正在使用 MVVM 模式开发 WPF 应用程序。
我正在将数据填充到 gridview 并在选择时填充到文本框、组合框以编辑内容。然后保存数据。
现在我想将新数据添加到网格视图中。用户对文本框的输入,组合框应添加到网格视图中,我需要将其保存到数据库中。
我的 XMAL 如下:
<Grid Height="444" Width="486">
<Label Content="Script" Height="28" HorizontalAlignment="Left" Margin="13,36,0,0" Name="lblScript" VerticalAlignment="Top" />
<TextBox Height="142" HorizontalAlignment="Left" Margin="75,38,0,0" Name="txtScript" VerticalAlignment="Top" Width="380" Text="{Binding Script_Text}"/>
<Button Content="Add" Command="{Binding SaveData}" Height="23" HorizontalAlignment="Left" Margin="284,409,0,0" Name="btnSave" VerticalAlignment="Top" Width="75" />
<Button Content="Reset" Command="{Binding ClearData}" Height="23" HorizontalAlignment="Left" Margin="380,409,0,0" Name="btnReset" VerticalAlignment="Top" Width="75" />
<DataGrid ItemsSource="{Binding Path=Param}" HeadersVisibility="Column" SelectionMode="Single"
AlternatingRowBackground="Gainsboro" Background="White" AutoGenerateColumns="False"
ItemContainerStyle="{StaticResource itemstyle}" CanUserAddRows="True" GridLinesVisibility="None"
Height="150" HorizontalAlignment="Left" HorizontalContentAlignment="Left" IsEnabled="True"
IsReadOnly="True" Margin="75,200,0,0" Name="dgMain" RowHeight="23" VerticalAlignment="Center"
VerticalContentAlignment="Center" Width="380" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding EditData}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Name}" Header="Parameter" Width="140" />
<DataGridTextColumn Binding="{Binding Path=Type}" Header="Type" Width="100" />
<DataGridTextColumn Binding="{Binding Path=Value}" Header="Value" Width="120" />
</DataGrid.Columns>
</DataGrid>
<Label Content="Name" Height="28" HorizontalAlignment="Left" Margin="75,186,0,0" Name="lblName" VerticalAlignment="Top" />
<TextBox Height="23" Text="{Binding SelectedItem.Name, ElementName=dgMain}" HorizontalAlignment="Left" Margin="75,213,0,0" Name="txtName" VerticalAlignment="Top" Width="110" />
<Label Content="Type" Height="28" HorizontalAlignment="Left" Margin="191,186,0,0" Name="lblType" VerticalAlignment="Top" />
<ComboBox Height="23" Text="{Binding SelectedItem.Type, ElementName=dgMain}" HorizontalAlignment="Left" Margin="191,213,0,0" Name="cboType" VerticalAlignment="Top" Width="110" ItemsSource="{Binding}">
<ComboBoxItem Content="integer" />
<ComboBoxItem Content="double" />
<ComboBoxItem Content="string" />
</ComboBox>
<Label Content="Value" Height="28" HorizontalAlignment="Left" Margin="307,186,0,0" Name="lblValue" VerticalAlignment="Top" />
<TextBox Height="23" Text="{Binding SelectedItem.Value, ElementName=dgMain}" HorizontalAlignment="Left" Margin="307,213,0,0" Name="textBox1" VerticalAlignment="Top" Width="110" />
<TextBlock HorizontalAlignment="Left" Margin="428,15,0,0" Height="32">
<ToggleButton x:Name="Add" Focusable="False" Command ="{Binding AddNew}" Style="{StaticResource SMToggle}" >
<Image Source="/Image/Add.png" Width="16" Height="16" />
</ToggleButton>
</TextBlock>
</Grid>
请帮我。序列号