带customedata源的wpf datagrid,在编辑后获取单元格值 [英] wpf datagrid with customedata source, get cell value after editing

查看:73
本文介绍了带customedata源的wpf datagrid,在编辑后获取单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将wpf DataGrid与customedata source一起使用.像打击一样的数据源:

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;
       }
}



在xaml代码中:



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);



我想在编辑后获取单元格文本值,但是在textbox的LostFocus事件(我将其放在单元格内)内部,在编辑之前我得到了相同的值.

编辑单元格后(单元格内有一个文本框),我怎么知道单元格的值?
我查找了许多文章,但是它们的保存更改仅在项目来源为数据库时才考虑.但在我的情况下,这是一个服装班.

有人可以告诉我在编辑后如何获取单元格值吗?



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!

推荐答案

在绑定上明确设置UpdateSourceTrigger ...
Explicitly set the UpdateSourceTrigger on your bindings...
<TextBox Text ="{Binding SourceSen,
                         UpdateSourceTrigger=LostFocus,
                         NotifyOnTargetUpdated=True}"
         TextWrapping="Wrap"
         LostFocus="SourceTextBox_LostFocus" />


这篇关于带customedata源的wpf datagrid,在编辑后获取单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆