C# 在silverlight 5中预填充组合框

C# 在silverlight 5中预填充组合框,c#,.net,silverlight-5.0,C#,.net,Silverlight 5.0,我需要从Silverlight中的代码中预先填充一个组合框,值从1到10,默认情况下所选的值应该是3。我该怎么办 private int _Rounds=3; [RequiredField] [MultipleChoice] public int Rounds { get { return this._Rounds; } set {

我需要从Silverlight中的代码中预先填充一个组合框,值从1到10,默认情况下所选的值应该是3。我该怎么办

 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");
                    }
                }
            }

这只是一个简单的示例,可以为您指出正确的方向,但可以在ViewModel中添加您可能的选项:

private readonly IEnumerable<int> roundOptions = Enumerable.Range(1, 10);
public IEnumerable<int> RoundOptions
{
    get
    {
        return roundOptions;
    }
}
private readonly IEnumerable roundOptions=Enumerable.Range(1,10);
公共IEnumerableRoundOptions
{
得到
{
返回选项;
}
}
然后绑定您的xaml:

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

这会将
RoundOptions
中包含的可能选项添加到组合框中,然后表示使用
双向绑定在ViewModel和UI之间保持
Rounds
变量同步。如果取舍选项将在ViewModel中更新为不同的选项集,我将使用
可观察集合


至少这是基于你的问题文本。我不知道
[multipleechoice]
属性的用途是什么。

@McGarnagle-我喜欢它。编辑。