首先想要实现数据绑定表格,我们在WPF中设计如下代码;
<Label Grid.Column="0" Grid.Row="0" Content="参数名称" FontSize="20" HorizontalAlignment="Right" VerticalAlignment="Center" />
<TextBox Grid.Column="1" Grid.Row="0" IsReadOnly="True" Margin="10,0,0,0" Name="txt_name" FontSize="20" Height="35" HorizontalContentAlignment="Center" />
<Label Grid.Column="2" Grid.Row="0" Content="参数值" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBox Grid.Column="3" Grid.Row="0" Margin="10,0,0,0" Name="txt_value" FontSize="20" VerticalAlignment="Center" HorizontalContentAlignment="Center" Height="35" />
<Button Grid.Column="4" IsEnabled="False" HorizontalAlignment="Center" Grid.Row="0" Width="100" Name="btn_save" Click="Btn_save_Click">保存</Button>
<Label HorizontalAlignment="Center" Name="lbl_msg" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="7" FontSize="20" Foreground="Red"></Label>
<DataGrid Grid.Row="1" MouseDoubleClick="DataResult_MouseDoubleClick" VirtualizingStackPanel.IsVirtualizing="False" BorderThickness="1" BorderBrush="#FFCFCFEA" Margin="5,0,0,0" Background="AliceBlue" FontSize="20" x:Name="dataResult" ItemsSource="{Binding}" AutoGenerateColumns="False"
SelectionUnit="FullRow" IsReadOnly="True" >
<DataGrid.Columns>
<DataGridTextColumn Width="*" Header="参数名称" ElementStyle="{StaticResource dgCell}" Binding="{Binding config_name}">
<DataGridColumn.HeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="AntiqueWhite" Offset="0.5"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontSize" Value="16"/>
<!--<Setter Property="Width" Value="300"/>-->
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</DataGridColumn.HeaderStyle>
</DataGridTextColumn>
<DataGridTextColumn Width="2*" Header="参数值" ElementStyle="{StaticResource dgCell}" Binding="{Binding config_value}">
<DataGridColumn.HeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="AntiqueWhite" Offset="0.5"/>
<!--<GradientStop Color="White" Offset="1"/>-->
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontSize" Value="16"/>
<!--<Setter Property="Width" Value="260"/>-->
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</DataGridColumn.HeaderStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
实现完成的界面效果如下:
绑定表格数据,c#代码如下:
//绑定表格数据 config_name, config_value
DataTable dt_parm = new DataTable();
void InitDataTable()
{
Dispatcher.Invoke(() =>
{
dt_parm.TableName = "dt_load"; //表名称
DataColumn dc_1 = new DataColumn("config_name", typeof(string)); //获取string类型的数据
DataColumn dc_2 = new DataColumn("config_value", typeof(string));
//添加进DataTable
dt_parm.Columns.Add(dc_1);
dt_parm.Columns.Add(dc_2);
//绑定表单
dataResult.DataContext = dt_parm;
});
}
然后使用MouseDoubleClick事件,实现双击后的逻辑:
//双击更新数据
private void DataResult_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
btn_save.IsEnabled = true;
lbl_msg.Content = " ";
DataRowView drv = dataResult.SelectedItems as DataRowView; //as将对象转换为指定类型
string config_name = drv["config_name"].ToString();
string config_value = drv["config_value"].ToString();
txt_name.Text = config_name;
//txt_code.Text = config_code;
txt_value.Text = config_value;
}
这样就实现了数据绑定表格。