Gokul
Gokul

Reputation: 1371

Prepopulate a combobox in silverlight 5

I need to pre populate a combobox from the code in Silverlight with values from 1- 10 and the selected value should be 3 by default. How do I do?

 private int _Rounds=3;

[RequiredField]
[MultipleChoice]
    public int Rounds
            {
                get { return this._Rounds; }
                set
                {
                    if (this._Rounds != value)
                    {
                        this.ValidateProperty("Rounds", value);
                        this._Rounds = value;
                        this.RaisePropertyChanged("Rounds");
                    }
                }
            }

Upvotes: 0

Views: 248

Answers (1)

McAden
McAden

Reputation: 13972

Just a quick example to point you in the right direction but add to your ViewModel your possible options:

private readonly IEnumerable<int> roundOptions = Enumerable.Range(1, 10);
public IEnumerable<int> RoundOptions
{
    get
    {
        return roundOptions;
    }
}

And then bind your xaml:

<ComboBox SelectedValue="{Binding Rounds, Mode=TwoWay}" ItemsSource="{Binding RoundOptions}" />

This adds to the ComboBox the possible options contained in RoundOptions and then says to keep the Rounds variable sync'd between the ViewModel and the UI using a TwoWay binding. If round options is going to be updated in the ViewModel to different sets of options I'd use an ObservableCollection instead.

At least that's based on your question text. I don't know what the [MultipleChoice] attribute is intended for.

Upvotes: 2

Related Questions