简体   繁体   中英

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

please find below thw xml code and c# code

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 :

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM