Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using wpf DataGrid with customedata source . the data source like blow:

public class SentencesField
   {
       public int No{get;set;}
       public string TargetSen { get; set; }
       public string SourceSen { get; set; }
       public static List<SentencesField> FillSentencesGrid(List<myXml.tu> m_list)
       {
           List<SentencesField> m_result = new List<SentencesField>();
           for (int i = 0; i < m_list.Count; i++)
           {
               SentencesField m_Field = new SentencesField();
               m_Field.No = i;
               m_Field.SourceSen = m_list[i].sourcelang;
               m_Field.TargetSen = m_list[i].targetlang;
               m_result.Add(m_Field);
           }
           return m_result;
       }
}


in xaml code:
<pre lang="xml"><DataGrid Name="m_SentencesFiled"
                       IsReadOnly="False"
                       AutoGenerateColumns="False" >
           <DataGrid.CellStyle>
               <Style TargetType="DataGridCell" >
                   <Style.Triggers>
                       <Trigger Property="IsKeyboardFocusWithin" Value="True">
                           <Setter Property="Background" Value="SeaGreen"/>
                           <Setter Property="BorderBrush" Value="Red"/>
                       </Trigger>
                   </Style.Triggers>
               </Style>
           </DataGrid.CellStyle>
           <!-- index column-->
           <DataGrid.Columns>
               <DataGridTextColumn Width="0.06*"  Binding="{Binding No}" IsReadOnly="True"  >
               </DataGridTextColumn>
               <!-- source sentences column-->
               <DataGridTemplateColumn Width="0.5*" >
                   <DataGridTemplateColumn.CellTemplate >
                       <DataTemplate>
                           <StackPanel>
                               <TextBox Text ="{Binding SourceSen,NotifyOnTargetUpdated=True}"
                                               TextWrapping="Wrap"
                                               LostFocus="SourceTextBox_LostFocus"
                                               />
                           </StackPanel>
                       </DataTemplate>
                   </DataGridTemplateColumn.CellTemplate>
               </DataGridTemplateColumn>
               <!-- target sentences column column-->
               <DataGridTemplateColumn Width="0.5*">
                   <DataGridTemplateColumn.CellTemplate >
                       <DataTemplate>
                           <StackPanel>
                               <TextBox Text ="{Binding TargetSen,NotifyOnTargetUpdated=True}"
                                               TextWrapping="Wrap"
                                               LostFocus="TargetTextBox_LostFocus"
                                             />
                           </StackPanel>
                       </DataTemplate>
                   </DataGridTemplateColumn.CellTemplate>
               </DataGridTemplateColumn>
           </DataGrid.Columns>
       </DataGrid>




when populating data grid

this.m_SentencesFiled.ItemsSource = SentencesField.FillSentencesGrid(m_SenList);


I want to get cell text value after editing, but inside the LostFocus event of textbox(which I put inside cell) I got same value before editing.

how could I know the cell value after editing a cell (there is a textbox inside cell)?
I look up many articles , but their saving changes in only consider when item source is database. but in my situation, it is a costume class.

could anyone tell me what to do in order to get the cell value after editing?

thanks in advance!
Posted

1 solution

Explicitly set the UpdateSourceTrigger on your bindings...
<TextBox Text ="{Binding SourceSen,
                         UpdateSourceTrigger=LostFocus,
                         NotifyOnTargetUpdated=True}"
         TextWrapping="Wrap"
         LostFocus="SourceTextBox_LostFocus" />
 
Share this answer
 
Comments
Alimjan Yasin 21-Jul-11 0:24am    
that worked, thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900