Click here to Skip to main content
15,667,010 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
It is necessary to implement a table (DataGrid), which is initially empty. It has 2 fields: The current date and the item selected from the ComboBox. How to refer to ComboBox'u if it's impossible by x:Name'u?
Here is an code:

plusBtn.Click += (source, e) =>
            {
                var tableStr = new SpecialDays
                (
                    date: DateTimeOffset.Now
                    //,timeTable: сюда нужно обратиться
                );
                timeTableData.Add(tableStr);
            };




XML
DataGridTemplateColumn Header="Суточное расписание"
                                                Width="50*">
 
                            <DataGridTemplateColumn.CellTemplate>
 
                                <DataTemplate>
 
                                    <TextBlock Text="{Binding Time}"/>
 
                                </DataTemplate>
 
                            </DataGridTemplateColumn.CellTemplate>
 
                            <DataGridTemplateColumn.CellEditingTemplate>
 
                                <DataTemplate>
 
                                    <ComboBox ItemsSource="{StaticResource TableList}"
                                              SelectedIndex="0"
                                              x:Name="cmb"
                                              SelectedItem="{Binding TimeTableList, Mode=OneWay}"/>
 
                                </DataTemplate>
 
                            </DataGridTemplateColumn.CellEditingTemplate>
 
                        </DataGridTemplateColumn>


Вот класс который создает экземпляр объекта:
C#
public class SpecialDays
        {
            public DateTimeOffset Date { get; set; }
            public int Time { get; set; }
            public ObservableCollection<int> TimeTableList { get => timeTableList; set => timeTableList = value; }
 
            private ObservableCollection<int> timeTableList = new();
 
            public SpecialDays(DateTimeOffset date, ObservableCollection<int> timeTable)
            {
                Date = date;
                TimeTableList = timeTable;
            }
            public SpecialDays()
            {
            }
 
            public SpecialDays(DateTimeOffset date, int timeTable)
            {
                Date = date;
                Time = timeTable;
            }
 
            //public SpecialDays(DateTimeOffset date, ObservableCollection<SpecialDays> timeTable) : this(date)
            //{
            //    this.timeTable = timeTable;
            //}
 
            public SpecialDays(DateTimeOffset date)
            {
                Date = date;
            }
        }


What I have tried:

<!--<DataGridComboBoxColumn Header="Суточное расписание"
                                                ItemsSource="{Binding TableList}"
                                                Width="50*"
                                                IsReadOnly="False"
                                                SelectedItemBinding="{Binding TimeTableList}"
                                                TextBinding="{Binding Time}"
                                                x:Name="cmbBox">
                            --><!--<DataGridComboBoxColumn.EditingElementStyle>
                                <Style TargetType="ComboBox">
                                    <Setter Property="ItemsSource" 
                                            Value="{StaticResource TableList}"/>
                                    <Setter Property="SelectedItem"
                                            Value="{Binding TimeTableList, Mode=OneWay}"/>
                                    <Setter Property="SelectedIndex"
                                            Value="0"/>
                                </Style>
                            </DataGridComboBoxColum<pre><pre lang="XML">
n.EditingElementStyle>-->
Posted
Updated 2-Aug-22 6:45am

1 solution

 
Share this answer
 
Comments
CIDEY 3-Aug-22 7:33am    
This solution uses ContentControl, but how to do it with DataGridTemplateColumn? How then can I contact the ComboBox so that it remembers the selected item?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900