WPF中数据绑定表格方法

首先想要实现数据绑定表格,我们在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; 
    }

这样就实现了数据绑定表格。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值