question

catalinabeaver-6852 avatar image
0 Votes"
catalinabeaver-6852 asked NicoZhu-MSFT commented

could not add data to the ComboBox from parametarised constructor

I'm trying to populate ComboBox. I'm able to add items from the default consctructor but not from the parameterised constructor.

  public  Dashboard()
         {
             this.InitializeComponent();
             MyCombobox1.Items.Add("item_constructor 1"); // this item is added successfully
         }
    
  public Dashboard(int count, string[] arr)
         {
             this.InitializeComponent();         
             local_arr = arr;
              for (int i = 0;i<count;i++)
              {
                  MyCombobox1.Items.Add(local_arr[i]); // not added..no error thrown                          
              }        
         }
    
  private async void button_Click(object sender, RoutedEventArgs e)
         {
           MyCombobox1.Items.Add("item_button_click"); // item added successfully
         }


The XAML code for the ComboBox..

 <ComboBox
                 Name="MyCombobox1"
                 x:FieldModifier="public"
                 Grid.Row="0"
                 Grid.Column="1"
                 Canvas.Left="-121"
                 Canvas.Top="150"
                 Margin="450,360,0,0"
                 Background="#F1F1F5"
                 FontSize="24"
                    
                 Header=" Choose file"
                 PlaceholderText="select"
                 RenderTransformOrigin="0.575,1.825"
                 SelectionChanged="MyCombobox1_SelectionChanged"
                  >
                    
             </ComboBox>

I'm not able to understand the mistake. Thank you.

dotnet-csharpwindows-uwpc++
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Show us the call to your function.
Did you check the values of count and arr with the debugger?

0 Votes 0 ·

The call to the parameterized constructor happens in other class. like below

 Dashboard d = new Dashboard(count, img_array);

The values in the variable count and arr are perfect. I have printed them by using

 System.Diagnostics.Debug.WriteLine(arr[i]);
 System.Diagnostics.Debug.WriteLine(count);

in the parameterised constructor

0 Votes 0 ·

1 Answer

karenpayneoregon avatar image
1 Vote"
karenpayneoregon answered NicoZhu-MSFT commented

Alternate, set the ItemSource

 ExampleComboBox.ItemsSource = new List<string>() { "First", "Second" };
 ExampleComboBox.SelectedIndex = 0;

Add item later

 var list = (List<string>)ExampleComboBox.ItemsSource;
 list.Add("Last");
 ExampleComboBox.SelectedIndex = list.Count - 1;


· 9
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I could only populate via a default constructor and a button click functions. My requirement is that the combobox (which is in class dashboard)should populate with data from other class(Main Page). This should be done as soon as I navigate to dashboard page from the Main page. The data domes to the dashboard from Main page via parameterized constructor.

0 Votes 0 ·
karenpayneoregon avatar image karenpayneoregon catalinabeaver-6852 ·

My example is conceptual, having valid list passed in from the constructor to your other class/window/page will work and this is basic programming not linked to a project type.

0 Votes 0 ·

Understood. I have tried adding data to the combobox as you mentioned from the parameterized constructor. I think the data is added. However, I could not see that data on the combobox. I have debug messages everywhere, I see this works as I wanted however, I don't see that on the GUI.

 namespace PROJECT
 {
       
     public sealed partial class MainPage : Page
     {
        public MainPage()
         {
                
             this.InitializeComponent();
        }
        private async void login_Click(object sender, RoutedEventArgs e)
         {
            int count = 3;
            string[] data = some array input;
            Dashboard d = new Dashboard(count, data);
         }
     }
 }

The above code is my mainpage class. Here I'm creating an object for the dashboard class and calling parameterized constructor. Below is my dashboard class code

0 Votes 0 ·
Show more comments