C# Can';t将值绑定到wpf中的组合框

C# Can';t将值绑定到wpf中的组合框,c#,wpf,xaml,C#,Wpf,Xaml,我有一个控件,combox在哪里。我从modelview绑定到它的属性。我可以将属性绑定到文本框,但不能绑定到组合框。modelview中的值为4。有人知道为什么吗 <ComboBox HorizontalAlignment="Left" VerticalAlignment="Top" SelectedItem="{Binding Path=QuantityOfStars}"> <ComboBoxIte

我有一个控件,combox在哪里。我从modelview绑定到它的属性。我可以将属性绑定到文本框,但不能绑定到组合框。modelview中的值为4。有人知道为什么吗

<ComboBox  HorizontalAlignment="Left" VerticalAlignment="Top" SelectedItem="{Binding Path=QuantityOfStars}">

                                    <ComboBoxItem Content="0"></ComboBoxItem>

                                    <ComboBoxItem Content="1"></ComboBoxItem>

                                    <ComboBoxItem Content="2"></ComboBoxItem>

                                    <ComboBoxItem Content="3"></ComboBoxItem>

                                    <ComboBoxItem Content="4"></ComboBoxItem>

                                    <ComboBoxItem Content="5"></ComboBoxItem>

                               </ComboBox>


public int QuantityOfStars
        {
            get
            {
                return this.ReporterHotelDescription.QuantityOfStars;

            }
            set
            {
                this.ReporterHotelDescription.QuantityOfStars = value;
                NotifyChanged("QuantityOfStars");
            }
        }

公共整数星的数量
{
得到
{
返回this.ReporterHotelDescription.QuantityOfStars;
}
设置
{
this.ReporterHotelDescription.QuantityOfStars=值;
通知更改(“STARS数量”);
}
}

您用ComboBoxItems(而不是整数)填充了ComboBoxItems,因此它无法将它们转换为整数以绑定到属性。手动用整数填充组合框:

<ComboBox 
    HorizontalAlignment="Left" VerticalAlignment="Top" SelectedItem="{Binding Path=QuantityOfStars}"
    xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <sys:Int32>0</sys:Int32>
    <sys:Int32>1</sys:Int32>
    <sys:Int32>2</sys:Int32>
    <sys:Int32>3</sys:Int32>
    <sys:Int32>4</sys:Int32>
    <sys:Int32>5</sys:Int32>
</ComboBox>

0
1.
2.
3.
4.
5.
或者,将组合框上的
ItemsSource
属性绑定到ViewModel中的一个属性,该属性是适当整数的列表