WPF/MVVM:组合框选择更改时,SelectedIndex绑定属性未更新 [英] WPF/MVVM: SelectedIndex Binding Property not updating when Combobox Selection Changed

查看:318
本文介绍了WPF/MVVM:组合框选择更改时,SelectedIndex绑定属性未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与 Jinesh的问题非常相似的问题.我需要在我的ViewModel代码中获取组合框的 SelectedIndex(或SelectedItem或SelectedValue)的值(即,当用户在组合框中选择一个项目时,我需要该索引(或项目或值)以更新ViewModel代码中的属性).我遵循了 Sheridan的答案 Felix C的答案,但是当所选项目发生更改时,我的财产没有得到更新.我正在尝试将新选择的索引值作为更新"按钮代码的一部分.

I've got a very similar question to Jinesh's. I need to get the value of the SelectedIndex (or SelectedItem or SelectedValue) of a combo box in to my ViewModel code (i.e., when a user selects an item in the combo box, I need that index (or item or value) to update a property in the ViewModel code). I've followed the suggestions in Sheridan's answer and Felix C's answer but my property is not getting updated when the selected item changes. I'm trying to get the newly selected index value as part of my "Update" button code.

查看:

<ComboBox Name="cboMonth"
          ItemsSource="{Binding MonthsList, Mode=OneTime}"
          DisplayMemberPath="month"
          SelectedIndex="{Binding MonthIndex, Mode=TwoWay}"/>

<Button Name="btnUpdate"
        Content="Update"
        Command="{Binding processUpdateButton}" />

ViewModel:

ViewModel:

public ObservableCollection<Month> MonthsList { get; set; }
private int _monthIndex;

public int MonthIndex
{
    get
    {                
        DateTime today = DateTime.Today;
        _monthIndex = today.Month - 1;
        return _monthIndex;
    }
    set
    {
        if (_monthIndex != value)
        {
            _monthIndex = value;
            RaisePropertyChanged("MonthIndex");
        }
    }
} 

public ICommand processUpdateButton
{
    get
    {
        if (_setUpdateButton == null)
        {
            _setUpdateButton = new RelayCommand(param => validateOdometer());
        }
            return _setUpdateButton;
        }            
    }

public void validateOdometer()
{
    Console.WriteLine("Validating odometer...");
    Console.WriteLine("Month Index: " + (_monthIndex));        
}

当我的页面首次呈现时,我的组合框默认为索引5(06月至6月),所讨论的属性_monthIndex反映为5.当我在组合框中选择一个新月(例如,十月)时,单击我的更新按钮(btnUpdate),_ monthIndex应该反映9,但仍然反映5.为什么?感谢任何/所有帮助.谢谢.

When my page first renders, I have the combo box defaulting to index 5 (06-June) and the property in question, _monthIndex, reflects 5. When I select a new month in the combo box (e.g., October) then click my update button (btnUpdate), _monthIndex should reflect 9 but it still reflects 5. Why? Appreciate any/all help. Thanks.

推荐答案

属性getter忽略先前设置的值,并且始终返回当前月份的索引.

The property getter ignores the previously set value and always returns the index of the current month.

声明应如下所示:

private int monthIndex = DateTime.Today.Month - 1;

public int MonthIndex
{
    get { return monthIndex; }
    set
    {
        if (monthIndex != value)
        {
            monthIndex = value;
            RaisePropertyChanged("MonthIndex");
        }
    }
} 

这篇关于WPF/MVVM:组合框选择更改时,SelectedIndex绑定属性未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆