1

I am trying to add the static values in to my combo box which is being written in WPF c#. I have following xaml piece of code which adds the items into my combo box.

<ComboBox Text="MyCombo">
<ComboBoxItem  Name="115200">Item1</ComboBoxItem>
<ComboBoxItem  Name="57600">Item2</ComboBoxItem>
<ComboBoxItem  Name="38400">Item3</ComboBoxItem>
</ComboBox>

But is there any way that I can use "ItemSource" property of the combo box in to my xaml code to populate the combo box or any other UI method to add the static values into combo box. Note: I do not want to do it in coding way to populate the values. I would like find the way of xaml or UI addition only.

Raj
  • 151
  • 1
  • 14
  • Where are the items supposed to be defined? You can for example bind ItemsSource to a static string collection property in some class. – Clemens Jul 17 '19 at 10:09
  • As I am new to C#/WPF, I do not have much idea of bind to static string collection. So would you please elaborate bit more clearly. – Raj Jul 17 '19 at 10:26
  • It's unclear what you are trying to achieve. So again, where are the items supposed to be defined? – Clemens Jul 17 '19 at 10:39
  • it should be defined in xaml file. Currently has been used in xaml file to define the items but I would prefer to use itemSource property in xaml file to define the items. – Raj Jul 17 '19 at 10:44
  • The ItemsSource property is not supposed to be populated in XAML. That's why there is the Items property, which is implicitly used when you write what you already have. It is however not required to use ComboBoxItems. You may as well use other types, e.g. `` (with the appropriate XAML namespace declaration). – Clemens Jul 17 '19 at 10:49
  • Ok thanks Clemens to clarify my doubt. – Raj Jul 17 '19 at 10:51
  • Please suggest me any other way of UI (adding items on collection string as we can do in WinForms) – Raj Jul 17 '19 at 10:52
  • For what reason? You said the items "*should be defined in xaml file*". That's what you have. If you can't tell us what you actually want to do, we can't help you. – Clemens Jul 17 '19 at 10:58
  • Hi Clemens, I just wanted to find the alternative way of defining than the source file and no specific reason. Also I manged to find the sys::string addition on the collection option of UI box to populate the elements. Thanks for your great help. – Raj Jul 17 '19 at 13:15
  • You are using `WPF` like `WinForms`, big no no! Look into MVVM instead and use Bindings to populate these items from your ViewModel. This will save you a lot of time in the long run! Trust me, many have tried using WPF like WinForms and FAILED MISERABLY! – XAMlMAX Jul 17 '19 at 14:12
  • Hi XAMlMAX , Can you explain to me bit further about "Bindings to populate these items from your ViewModel" as I am new to C#? – Raj Jul 17 '19 at 14:17
  • [MVVM tutorial](https://www.tutorialspoint.com/mvvm/index.htm) this should set you on a right track. @Raj – XAMlMAX Jul 17 '19 at 15:25

1 Answers1

1

You can bind your Combo box items from your view model using item source.

See the example below:

First, you want to set the DataContext of your Window.

/// <summary> 
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModel();
    }
}

Next,

public class ViewModel
{
    public ObservableCollection<string> CmbContent { get; private set; }

    public ViewModel()
    {
        CmbContent = new ObservableCollection<string>
        {
            "Item 1", 
            "Item 2",
            "Item 2"

        };
    }
}

Finally,

<Grid>
    <ComboBox Width="200"
          VerticalAlignment="Center"
          HorizontalAlignment="Center"
          x:Name="MyCombo"
          ItemsSource="{Binding CmbContent}" />
</Grid>