简体   繁体   English

当我按下网格行中的按钮时,获取更新的单元格值

[英]Get the updated cell value when i press the buttom in the grid row

i have a datagrid in wpf contains text column and button column with c# code when i update the cell and press the button in the row it takes the original value from datagrid sourceItems but if i Pressed Enter then i went back to column and pressed the button it takes the updated value 我在wpf中有一个datagrid,包含带有c#代码的文本列和按钮列,当我更新单元格并按下行中的按钮时,它从datagrid sourceItems获取原始值但是如果我按下Enter然后我回到列并按下按钮它需要更新的值

please find below thw xml code and c# code 请在下面找到xml代码和c#代码

I tried to get the selected row the take the values but the problem same it takes the values from source items 我试图让所选行获取值,但问题与源项目中的值相同

 <DataGrid x:Name="GrdExamCheck" AutoGenerateColumns="False" HorizontalAlignment="Left" Background="{x:Null}" VerticalAlignment="Stretch" Canvas.Top="10" FontFamily="Times New Roman" Width="1079" FontWeight="Bold" Canvas.Left="10" >
                <DataGrid.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Setter Property="TextBlock.TextAlignment" Value="Center"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type DataGridCell}">
                                    <Grid Background="{TemplateBinding Background}">
                                        <ContentPresenter VerticalAlignment="Bottom"/>
                                    </Grid>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </DataGrid.CellStyle>
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Student ID" Visibility="Hidden" Binding="{Binding StuId}"/>
                    <DataGridTemplateColumn Header="Student" Width="180" IsReadOnly="True">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <Image Source="{Binding StuImage}" Width="50" Height="50" />
                                    <TextBlock Text="{Binding StuName}" VerticalAlignment="Bottom" FontFamily="Times New Roman" FontWeight="Bold" FontSize="13"/>
                                </StackPanel>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTextColumn Header="Mark" Width="100" Binding="{Binding CheckMark}"></DataGridTextColumn>
                    <DataGridTemplateColumn Header="Factor" Width="100" IsReadOnly="True">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{Binding Factor}" TextAlignment="Center"  VerticalAlignment="Bottom" FontFamily="Times New Roman" FontWeight="Bold" FontSize="13"/>
                                </StackPanel>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="Versions" Width="100" IsReadOnly="True">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{Binding CheckUpdated}" VerticalAlignment="Bottom" TextAlignment="Center"  FontFamily="Times New Roman" FontWeight="Bold" FontSize="13"/>
                                </StackPanel>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="Remark" Width="200" >
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBox Text="{Binding Remark}" VerticalAlignment="Bottom"  HorizontalAlignment="Stretch" FontFamily="Times New Roman" FontWeight="Bold" FontSize="13" Foreground="Black" BorderBrush="{x:Null}" Background="{x:Null}"/>
                                </StackPanel>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="Absent" Width="70">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button x:Name="BtnAbsent" Content=""  Width="40" Height="40" BorderBrush="{x:Null}" Click="BtnAbsent_Click" >
                                    <Button.Background>
                                        <ImageBrush ImageSource="/School Manager v001;component/Pics/absent.ico" Stretch="Uniform"/>
                                    </Button.Background>
                                </Button>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="Submit" Width="70">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button x:Name="BtnSubmit" Content=""  Width="40" Height="40" BorderBrush="{x:Null}" Click="BtnSubmit_Click" >
                                    <Button.Background>
                                        <ImageBrush ImageSource="/School Manager v001;component/Pics/submitMark.ico" Stretch="Uniform"/>
                                    </Button.Background>
                                </Button>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="Attach" Width="70">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button x:Name="BtnAttach" Content=""  Width="40" Height="40" BorderBrush="{x:Null}" Click="BtnAttach_Click" >
                                    <Button.Background>
                                        <ImageBrush ImageSource="/School Manager v001;component/Pics/attachment.ico" Stretch="Uniform"/>
                                    </Button.Background>
                                </Button>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>

c# code : c#代码:

private void BtnSubmit_Click(object sender, RoutedEventArgs e)
    {
        var mark = GrdExamCheck.CurrentItem as Mark;
        if (mark != null)
        {
            InsertMark(int.Parse(LbExamIdContent.Content.ToString()), mark.StuId, DateTime.Now.ToShortDateString(), mark.Remark, Convert.ToDecimal(mark.CheckMark), Convert.ToDecimal(LbFactorContent.Content.ToString()));
        }
        FillClassStudentList(int.Parse(LbCLassIdContent.Content.ToString()));


    }

I need to get the current text from Mark column if i edit the text and directly pressed the submit button 如果我编辑文本并直接按下提交按钮,我需要从Mark列获取当前文本

当属性更改时,您必须触发绑定源的更新:

 Binding="{Binding Path=CheckMark, UpdateSourceTrigger=PropertyChanged}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我按 Enter 时,dataGridView 获取更新单元格的值 - dataGridView get the value of an updated cell when I press enter 为什么在单击网格行时得到错误的值? - Why do I get the wrong value when I click on a grid row? 单击特定行时,如何获取gridControl的单元格值? - How can I get The cell value of the gridControl when I click on a specific row? WPF:网格布局-当光标在空单元格上方时,如何通过MouseMove或类似事件获取元素的行和列? - WPF: Grid layout - how can I get row and column of element with MouseMove or similar events, when cursor is over empty cell? 当我双击WPF DataGrid行以获取单元格值时,返回NULL - When i double click on WPF DataGrid row for get cell value, return NULL 动态修改时不更新 Datagridview 单元格值 - Datagridview cell value not updated when modified dynamically 按钮单击行时,在C#Gridview中获取单元格值 - Get Cell Value in C# Gridview when Button clicked on row 在WPF表单中,在ShowSearchPanelMode中按键后,选中了网格第一行中的我的复选框,在移动到网格行之前我不希望进行检查? - In WPF form My check box in first row of grid get checked after key press in ShowSearchPanelMode , I Don't want to check until i move to grid row? 获取 GridView 行的单元格值 - Get the cell value of a GridView row 在DataGridView中获取单元格行的值 - Get Value of Cell Row in DataGridView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM