M
M
Mikhailushka2018-11-28 23:46:22
WPF
Mikhailushka, 2018-11-28 23:46:22

How to designate a ComboBox in the current context?

Hello everyone, I'm just getting started with wpf, maybe a very stupid question, but I've been scratching my head all day and can't figure out what the problem is.
I created a combobox in a DataTemplate, but when I try to somehow manipulate it from a .cs file, I get an error: The name "categoryComboBox" does not exist in the current context.
Here is the XAML code

<DataGrid x:Name="addDataGrid" HorizontalAlignment="Left" ItemsSource="{Binding}" Height="517" VerticalAlignment="Top" Width="975" Initialized="Window_Initialized_Add" AutoGenerateColumns="False">
                        <DataGrid.Columns>
                            <DataGridTemplateColumn Header="Категории">
                                <DataGridTemplateColumn.CellEditingTemplate>
                                    <DataTemplate>
                                        <ComboBox x:Name="categoryComboBox"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellEditingTemplate>
                            </DataGridTemplateColumn>
                        </DataGrid.Columns>
                    </DataGrid>

Here is the .cs code
//combobox

        private MySqlDataAdapter categoryaddDataAdapter;

        private void fillCategoryComboBox()
        {
            categoryaddDataAdapter = new MySqlDataAdapter
            {
                SelectCommand = new MySqlCommand()
                {
                    Connection = programQuery,
                    CommandText = "SELECT * FROM category"
                }
            };
            categoryaddDataAdapter.Fill(addDataSet, "category");
            DataTable dtEmpName = addDataSet.Tables["category"];
            categoryComboBox.ItemsSource = ((IListSource)dtEmpName).GetList();
            categoryComboBox.SelectedValuePath = "id_category";
            categoryComboBox.DisplayMemberPath = "name_category";
        }

How can I access the categoryComboBox?
I tried to write addDataGrid.categoryComboBox, but I get an error: 'DataGrid' does not contain a definition for 'categoryComboBox', and could not find an available extension method 'categoryComboBox' that accepts the type 'DataGrid' as the first argument (maybe a using directive or a reference to assembly).

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question