Search code examples
c#wpfdatatemplate

change textblock text Only applies to the first record


hi i have datatemplate and textblock inside it, i want when user checked checkbox my textblock text change i did it this way:

<DataGridTemplateColumn Header="نمره" MinWidth="150" Width="auto">
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <StackPanel Orientation="Vertical" Margin="5" HorizontalAlignment="Right" ToggleButton.Checked="StackPanel_Checked">
                                                    <Metro:MetroSwitch Checked="chkChecked_Checked" Tag="exc" Margin="0,2">خیلی خوب</Metro:MetroSwitch>
                                                    <Metro:MetroSwitch Checked="chkChecked_Checked" Tag="good" Margin="0,2">خوب</Metro:MetroSwitch>
                                            </StackPanel>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>
                                <DataGridTemplateColumn Header="وضعیت" MinWidth="100" Width="auto">
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate x:Name="myDataTemplate">
                                            <StackPanel Orientation="Vertical" Margin="5" HorizontalAlignment="Center">
                                                <Metro:MetroTextBlock Text="ثبت نشده" Foreground="Red" Name="txtStatus"/>
                                            </StackPanel>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>

and code-behind:

private void chkChecked_Checked(object sender, RoutedEventArgs e)
        {
            Arthas.Controls.Metro.MetroTextBlock temp = FindVisualChildByName<Arthas.Controls.Metro.MetroTextBlock>(dataGrid, "txtStatus");


            switch ((sender as Arthas.Controls.Metro.MetroSwitch).Tag.ToString())
            {
                case "exc":
                    temp.Foreground = new SolidColorBrush(Colors.Green);
                    temp.Text = "شده";

                    break;
                case "good":
                    temp.Foreground = new SolidColorBrush(Colors.Green);
                    temp.Text = "شده";
                    break;    
            }
        }

But the problem is that only the first record will change
Each record contains 2 checkboxes and 1 textblock, By selecting the checkbox in each row, the text of that textblock must be changed But my codes only work on the first row


Solution

  • I think that may be because you are trying to find the textblock by name in the entire dataGrid. It will always return the first control with the same name it finds.

    You should find the textblock in the row where the checkbox is present.

    In your method add the following line:

    private void chkChecked_Checked(object sender, RoutedEventArgs e)
    {
    // this will find the datagrid item
    var row = dataGrid.ContainerFromElement(sender as DependencyObject);
    // now use this datagrid item to find your textblock
    Arthas.Controls.Metro.MetroTextBlock temp = FindVisualChildByName<Arthas.Controls.Metro.MetroTextBlock>(row, "txtStatus");
    }