1

I have a datagrid with a column containing a ComboBox. I've set the Name for my combobox, but this name is not visible in code, why?

<DataGrid ...>
    <DataGrid.Columns>
        <DataGrid.TemplateColumn>
             <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox Name="mex" Style="{DynamicResource ComboBoxStyle}"
                              ItemsSource="{Binding Path=combolist}"
                              SelectionChanged="status_SelectionChanged" Height="auto" Width="Auto">
                    </ComboBox>
                </DataTemplate>
             </DataGridTemplateColumn.CellTemplate>
        </DataGrid.TemplateColumn>
        ...
    </DataGrid.Columns>
</DataGrid>

In C# code mex is empty, not visible, why?

I tried x:Name="mex" as well, but it is still not visible.

c#:

mex.ItemsSource = dt;

undifined mex

H.B.
  • 166,899
  • 29
  • 327
  • 400
MexVl
  • 19
  • 5
  • Where is this `ComboBox`? MainWindow.xaml? App.xaml? A resource dictionary? – user7116 Jul 10 '12 at 13:43
  • ComboBox insert datagrid – MexVl Jul 10 '12 at 13:45
  • That's not terribly specific. Could you show us more code? I'm pretty sure I know what your problem is, but I am not 100% certain with your snippet. – user7116 Jul 10 '12 at 13:47
  • @MexVl I made an edit to your question based on what you wrote which I hope will clarify the question. Feel free to roll it back if it is not correct. Also, can you post the C# code you are using to access `mex`? – Rachel Jul 10 '12 at 13:54
  • You should have a lot of these combobox : one per column. which one did you expect your variable would have referenced ? – Kek Jul 10 '12 at 14:09

3 Answers3

3

A DataGridColumn is never actually in the Logical or Visual Tree; it is always a DataGridRow with DataGridCells, since they are automatically created for each row in the DataGrid.

The only way to reach your component is to build a complex Binding or find it using a Logical or Visual Tree helper.

BTW you should set your ItemsSource of your ComboBox through Bindings to available data from your row. You can't create bindings using ElementName within a DataGridTemplateColumn as again it is not in the Logical or Visual Tree.

I found an interesting link that explains the Visual Tree of a DataGrid: http://blogs.msdn.com/b/vinsibal/archive/2008/08/14/wpf-datagrid-dissecting-the-visual-layout.aspx.

Kolky
  • 2,917
  • 1
  • 21
  • 42
0

You cannot directly reference elements when they are stored within an items template. Normally you'd want to allow the viewmodel to handle the bindings by setting the datacontext to the parent object and then allow the elements within the items template to pick this up. However based upon your question it looks as if you are attempting to do this directly from code behind.

Here are two similar questions and solutions for setting up the datacontext of an item as well as referencing an element within an items template. Hope this helps

Access parent DataContext from DataTemplate

and

WPF - ItemsControl - How do I get find my "CheckBox" item that is in the ItemTemplate?

Community
  • 1
  • 1
rlcrews
  • 3,482
  • 19
  • 66
  • 116
0

because template is used by all rows.if name is usable .must have the same name in visual tree at one time.

gyluo
  • 1