How do I get access to the property of children inside of a DataGrid by the x:Name in WPF?

Mojtaba_Hakim 281 Reputation points
2022-08-28T08:35:27.043+00:00

I want to control the children's elements of a DataGrid in WPF for example a ComboBox, like an ordinary element like this: ComboBox1.Text = "test";

What have I tried :

<Grid x:Name="mygrid">  
     <DataGrid x:Name="MainDataGrid">  
      
                <DataGrid.Columns>  
                    <DataGridTextColumn Header="The Name :" Width="120" Binding="{Binding NAMES, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" />  
      
                    <DataGridTemplateColumn Header="CMB" Width="150">  
                        <DataGridTemplateColumn.CellTemplate>  
                            <DataTemplate>  
                                <ComboBox x:Name="ComboBox1"  
                                      
                                      SelectedValuePath="CODE"  
                                      DisplayMemberPath="NAMES"  
                                      IsTextSearchEnabled="True"  
                                      SelectedIndex="0"  
                                      IsEditable="False"  
                                      SelectionChanged="ComboBox1_SelectionChanged"  
                                      BorderBrush="#FFADEEB4" Background="{x:Null}" BorderThickness="1">  
      
                                    <ComboBox.ItemsPanel>  
                                        <ItemsPanelTemplate>  
                                            <VirtualizingStackPanel VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling"/>  
                                        </ItemsPanelTemplate>  
                                    </ComboBox.ItemsPanel>  
                                </ComboBox>  
                            </DataTemplate>  
                        </DataGridTemplateColumn.CellTemplate>  
                    </DataGridTemplateColumn>  
                </DataGrid.Columns>  
      
            </DataGrid>  
</Grid>  

CS:

private void Window_Loaded(object sender, RoutedEventArgs e)  
        {  
            
            var Test0 = this.FindName("ComboBox1");  
            var Test1 = mygrid.Children.OfType<ComboBox>().FirstOrDefault();  
            var Test2 = GetChildOfType<ComboBox>(mygrid);  
            var Test3 = FindChild<ComboBox>(Application.Current.MainWindow, "ComboBox1");  
  
        }  

All of the tests are Null!

Whats is the problem ?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,670 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
762 questions
{count} votes

Accepted answer
  1. Jack J Jun 24,286 Reputation points Microsoft Vendor
    2022-08-29T07:31:33.933+00:00

    @Mojtaba_Hakim , Welcome to Microsoft Q&A, you could try the following code to get the combox property in the DataTemplate.

     private void Window_Loaded(object sender, RoutedEventArgs e)  
            {  
                var combo = FindControl<ComboBox>(this, typeof(ComboBox), "ComboBox1");  
                Console.WriteLine(combo.Text);  
            }  
        public static T FindControl<T>(UIElement parent, Type targetType, string ControlName) where T : FrameworkElement  
            {  
      
                if (parent == null) return null;  
      
                if (parent.GetType() == targetType && ((T)parent).Name == ControlName)  
                {  
                    return (T)parent;  
                }  
                T result = null;  
                int count = VisualTreeHelper.GetChildrenCount(parent);  
                for (int i = 0; i < count; i++)  
                {  
                    UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);  
      
                    if (FindControl<T>(child, targetType, ControlName) != null)  
                    {  
                        result = FindControl<T>(child, targetType, ControlName);  
                        break;  
                    }  
                }  
                return result;  
            }  
    

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful