1

I am trying to select ComboBox name. But when the Combobox is in a DataTemplate, I can't reach it. Can you help me?

When the Combobox is not in a DataTemplate, I can select dot. in .cs file:

-XAML-
<StackPanel>
  <ComboBox Grid.Column="0" Grid.Row="0" Name="dot" DropDownClosed="dot_DropDownClosed" />
</StackPanel>

-.CS-
private void vove_DropDownClosed(object sender, EventArgs e)
 {
   var tableTopLetter = dot.SelectedValue.ToString();
   MessageBox.Show(tableTopLetter);
 }

When the Combobox is in a DataTemplate:

<DataGridTemplateColumn x:Name="Cobo" Header="TableTop Type">
   <DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
         <ComboBox x:Name="Cobo3" ItemsSource="{Binding strArray}"  >
            <ComboBoxItem Content="A" />
            <ComboBoxItem Content="B" />
            <ComboBoxItem Content="C" />
         </ComboBox>
      </DataTemplate>
   </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

I can't select "Cobo3" in .cs

public mFlow()
    {
        InitializeComponent();
        Cobo3. '<--- can't find
    }
Jennifer S
  • 1,419
  • 1
  • 24
  • 43
  • I strongly suggest using MVVM pattern and zero code-behind. finding object hierarchy like this is such a headache – Bizhan Feb 05 '20 at 20:51
  • you cant, it depends what you are trying to do you should find another solution – Arsen Mkrtchyan Feb 05 '20 at 20:56
  • @ArsenMkrtchyan you can't what, find the combo in a DataTemplate, I beg to differ if that's what you mean. I do although agree about a different solution, MVVM is better suited. – Trevor Feb 05 '20 at 20:59
  • 1
    @Çöđěxěŕ agree with you, it is possible to find it in visual tree... or may be use template part... but it all depends from situation... what I wanted to say, that overall it is not good practice – Arsen Mkrtchyan Feb 05 '20 at 21:19
  • @ Raimondas A: Look at the following post: [How do I access a control inside a XAML DataTemplate?](https://stackoverflow.com/questions/16375375/how-do-i-access-a-control-inside-a-xaml-datatemplate). – Jackdaw Feb 05 '20 at 21:54
  • @Jackdaw Thanks, that was what I need to resolve my problem! – Raimondas A Feb 07 '20 at 07:10

0 Answers0