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

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