简体   繁体   English

无法将值绑定到WPF中的组合框

[英]Can't bind value to combobox in wpf

I have a control,where is combox. 我有一个控件,哪里是combox。 I bind to it properties from modelview. 我从模型视图绑定到它的属性。 I can bind properties to textboxes, but not to combobox. 我可以将属性绑定到文本框,但不能绑定到组合框。 Value from modelview is 4. Anyone know why is that ? 来自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");
            }
        }

You filled your ComboBox with ComboBoxItems, not integers, so it cannot convert them to an integer to bind to your property. 您用ComboBoxItems(而不是整数)填充了ComboBox,因此它无法将它们转换为整数以绑定到您的属性。 Either fill the ComboBox with integers manually: 手动用整数填充ComboBox:

<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>

Or, bind the ItemsSource property on the ComboBox to a property in your ViewModel that is a list of the appropriate integers. 或者,将ComboBox上的ItemsSource属性绑定到ViewModel中的属性,该属性是适当整数的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM