C# WPF - editable column in a datagrid -


problem description

i have datagrid named data_grid_1, has 2 columns header1 , header2 , filled someclass-object's inside data_grid_1 loaded-event-handler.

xaml-code:

<window x:class="wpfapplication1.mainwindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         title="mainwindow" height="350" width="525">      <grid>         <datagrid x:name="data_grid_1"                   horizontalalignment="left"                   margin="10,10,0,0"                    verticalalignment="top"                   height="303" width="497"                    loaded="data_grid_1_loaded">             <datagrid.columns>                 <datagridtextcolumn header="header1"                                      binding="{binding some_field_variable_2, mode=oneway}" />                 <datagridtextcolumn header="header2"                                      binding="{binding some_field_variable_3, mode=oneway}" />             </datagrid.columns>      </datagrid> </grid> </window> 

xaml.cs-code:

namespace wpfapplication1 {     public partial class mainwindow : window     {         public mainwindow()         {             initializecomponent();         }          private void data_grid_1_loaded(object sender, routedeventargs e)         {              data_grid_1.items.add(new someclass(1, "data_grid_1-string1", "data_grid_1-text1"));             data_grid_1.items.add(new someclass(2, "data_grid_1-string2", "data_grid_1-text2"));             data_grid_1.items.add(new someclass(3, "data_grid_1-string3", "data_grid_1-text3"));             data_grid_1.items.add(new someclass(4, "data_grid_1-string4", "data_grid_1-text4"));         }     } } 

someclass.cs

namespace wpfapplication1 { class someclass {     public int some_field_variable_1 { get; internal set; }     public string some_field_variable_2 { get; internal set; }     public string some_field_variable_3 { get; internal set; }      public someclass(int some_field_variable_1,              string some_field_variable_2,              string some_field_variable_3)     {         this.some_field_variable_1 = some_field_variable_1;         this.some_field_variable_2 = some_field_variable_2;         this.some_field_variable_3 = some_field_variable_3;     } } } 

question

i want make header2-column editable, if user clicks , changes value of row in header2-column changes written someclass-object, added inside data_grid_1_loaded.

how can make second column editable?

public partial class mainwindow : window    {        public mainwindow()     {         initializecomponent();     }      public static observablecollection<someclass> getitems()     {         observablecollection<someclass> some_inner_object_list = new observablecollection<someclass>();         some_inner_object_list.add(new someclass(1, "data_grid_1-string1", "data_grid_1-text1"));         some_inner_object_list.add(new someclass(2, "data_grid_1-string2", "data_grid_1-text2"));         some_inner_object_list.add(new someclass(3, "data_grid_1-string3", "data_grid_1-text3"));         some_inner_object_list.add(new someclass(4, "data_grid_1-string4", "data_grid_1-text4"));          return some_inner_object_list;     }     private void data_grid_1_loaded(object sender, routedeventargs e)     {         data_grid_1.itemssource = getitems();      }     private void data_grid_1_selectedcellschanged(object sender, selectedcellschangedeventargs e)     {         if (e.addedcells.count == 0) return;         var currentcell = e.addedcells[0];         if (currentcell.column ==             data_grid_1.columns[1])         {             data_grid_1.beginedit();         }     } } 

xaml-code:

<datagrid x:name="data_grid_1"                autogeneratecolumns="false"                canuseraddrows="false"                selectionmode="extended"                selectionunit="cell"                loaded="data_grid_1_loaded"               selectedcellschanged="data_grid_1_selectedcellschanged" margin="0,12,12,77">         <datagrid.columns>             <datagridtextcolumn header="header1" binding="{binding some_field_variable_2, mode=oneway}" width="1*" />             <datagridtemplatecolumn header="header2" width="100">                 <datagridtemplatecolumn.celltemplate>                     <datatemplate>                         <textblock text="{binding some_field_variable_3}" />                     </datatemplate>                 </datagridtemplatecolumn.celltemplate>                 <datagridtemplatecolumn.celleditingtemplate>                     <datatemplate>                         <grid>                             <grid.columndefinitions>                                 <columndefinition width="1*" />                                 <columndefinition width="auto" />                             </grid.columndefinitions>                             <textbox text="{binding some_field_variable_3}" />                             <textblock grid.column="1" />                         </grid>                     </datatemplate>                 </datagridtemplatecolumn.celleditingtemplate>             </datagridtemplatecolumn>          </datagrid.columns>     </datagrid> 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -