wpf 设置 DataGrid中的某一列可以编辑

本文介绍了如何使用C#编程语言创建一个数据网格控件,并展示了如何通过设置属性来定制其外观和交互行为,包括设置列、绑定数据源以及调整样式。通过实例演示了如何实现数据的可视化呈现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、效果:

2、页面代码

  <DataGrid Name="dataGrid" AutoGenerateColumns="False" CanUserAddRows="False"   >             <DataGrid.Columns>                 <DataGridTextColumn Header="ID"

                   Binding="{Binding ID}"

                   Width="*" IsReadOnly="True">                 </DataGridTextColumn>                 <DataGridTextColumn Header="Msg"

                   Binding="{Binding Msg}"

                   Width="*">

                    <DataGridTextColumn.ElementStyle>

                        <Style TargetType="TextBlock">

                            <Setter Property="Foreground" Value="Red"/>

                            <Setter Property="TextAlignment" Value="Center"/>

                        </Style>

                    </DataGridTextColumn.ElementStyle>

                    <DataGridTextColumn.EditingElementStyle>

                        <Style TargetType="TextBox">

                            <Setter Property="Foreground" Value="White"/>

                            <Setter Property="Background" Value="Gray"/>

                            <Setter Property="BorderBrush" Value="Navy"/>

                            <Setter Property="BorderThickness" Value="2"/>

                        </Style>

                    </DataGridTextColumn.EditingElementStyle>

                </DataGridTextColumn>        

            </DataGrid.Columns>         

</DataGrid>

3、后台代码

    DataTable dt;      

   public MainWindow()    

     {      

       InitializeComponent();         

    dt = new DataTable();          

   DataColumn d1 = new DataColumn("ID", typeof(string));          

   dt.Columns.Add(d1);            

d1 = new DataColumn("Msg", typeof(string));      

       dt.Columns.Add(d1);

            DataRow dr;  

           for (int n = 0; n < 99; n++)        

     {               

  dr = dt.NewRow();               

  dr[0] = (n + 1).ToString();        

         dr[1] = "信息" + (n + 1).ToString();        

         dt.Rows.Add(dr);

            }            

dataGrid.ItemsSource = dt.DefaultView;          

        }

 

转载于:https://www.cnblogs.com/liwenxue88/p/3443491.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值