带有SelectedValue的WPF Combobox表现得很奇怪

问题描述 投票:0回答:1

我有一个包含一定年限的ComboBox。但是,我的项目有问题,应该选择。

XAML

 <ComboBox MaxDropDownHeight="90" Height="10" Width="80" ItemsSource="{Binding accy}"  DisplayMemberPath="DisplayMember" SelectedValuePath="ValuePath" SelectedValue="{Binding Accy_selected}"/>

MainWindow.cs

 public MainWindow()
    {
        InitializeComponent();           
        this.DataContext = new Test();}

test.cs中

class Test
{
    public ObservableCollection<ComboYear> accy { get; set; }
    private string _accy_selected = "";

    public Test()
    {
        int min = 1940;
        int max = 2040;
        accy = new ObservableCollection<ComboYear>();                                    
            for (int i = min; i < max; i++)
            {
                accy.Add(new ComboYear { DisplayMember = Convert.ToString(i), ValuePath = Convert.ToString(i) });
            }
            Accy_selected = Convert.ToString(max);                     
    }

        public class ComboYear
        {
            public string DisplayMember { get; set; }
            public string ValuePath { get; set; }
        }       

    public string Accy_selected
    {
        get { return _accy_selected; }
        set
        {
            if (value != _accy_selected)
            {
                _accy_selected = value;
                NotifyPropertyChanged("Accy_selected");
            }

        }
    }

ComboBox被填满,但没有选择任何项目。关于这一点的奇怪之处在于我是否改变了这一行

Accy_selected = Convert.ToString(max);

它正在发挥作用。它每年都在ComboBox工作。将int转换成string我真傻吗?

Accy_selected = "2039";

我不知道如何解决它

c# wpf data-binding combobox selected
1个回答
2
投票

qazxsw poi(qazxsw poi)不包括在qazxsw poi中,是吗?只要计数器变量max小于2040,你就在迭代:

ComboBox

如果将条件更改为i,则在设置max属性时应包含并选择值“2040”:

for (int i = min; i < max; i++)
© www.soinside.com 2019 - 2024. All rights reserved.