ComboBox中的SelectedValue始终返回0

时间:2013-11-18 16:40:00

标签: c# wpf mvvm combobox enums

我正在使用Enum类来允许自己轻松地从Enum转换为Description。我知道还有其他方法可以做到这一点,但这是我公司过去做过的事情所以我需要坚持相同的结构。

这是我的枚举类:

 [TypeConverter(typeof(EnumToStringUsingDescription))]    
    public enum ArTypes
    {
        [Description("Adjustment")]
        [EnumInformation("Adjustment", true, 1)]
        arAdjustment = 1,
        [Description("Payment")]
        [EnumInformation("Payment", true, 2)]
        arPayment = 3,
        [Description("Deposit Receipt")]
        [EnumInformation("Deposit Receipt", true, 3)]
        arDepositReceipt = 5,
        [Description("Deposit Applied")]
        [EnumInformation("Deposit Applied", true, 4)]
        arDepositApplied = 7,
        [Description("Bad Debt Transfer")]
        [EnumInformation("Bad Debt Transfer", true, 5)]
        arBadDebtTransfer = 9,
        [Description("Bad Debt Writeoff")]
        [EnumInformation("Bad Debt Writeoff", true, 6)]
        arBadDebtWriteoff = 11,
        [Description("Bad Debt Recovery")]
        [EnumInformation("Bad Debt Recovery", true, 7)]
        arBadDebtRecovery = 13,
        [Description("Charge")]
        [EnumInformation("Charge", true, 8)]
        arCharge = 15,
        [Description("Immediate Case Receipt")]
        [EnumInformation("Immediate Cash Receipt", true, 9)]
        arImmediateCashReceipt = 17,
        [Description("Over Payment")]
        [EnumInformation("Over Payment", true, 10)]
        arOverPayment = 19,
        [Description("Balance Forward")]
        [EnumInformation("Balance Forward", true, 11)]
        arBalanceForward = 21,
    }

XAML:

<Label VerticalAlignment="Center" Margin="5,0,0,0" Content="ArType: " Grid.Row="5" Grid.Column="0"></Label>
        <telerik:RadComboBox ItemsSource="{Binding ArTypeList}"
                             DisplayMemberPath="Key"
                             SelectedValuePath="Value"
                             HorizontalAlignment="Left" Width="190"
                             SelectedValue="{Binding Path=SelectedArType, Mode=TwoWay, ValidatesOnDataErrors=True}" 
                             TabIndex="5"  Grid.Row="5" VerticalAlignment="Center" Grid.Column="1"
                             Style="{StaticResource RadComboBoxStyle}" />

TotalAdjustmentsOptionsViewModel:

private ObservableCollection<KeyValuePair<String, ArTypes>> _ArTypeList;
public ObservableCollection<KeyValuePair<String, ArTypes>> ArTypeList
{
    get
    {
        if (_ArTypeList == null)
        {

            _ArTypeList = new ObservableCollection<KeyValuePair<string, ArTypes>>();

            EnumToStringUsingDescription converter = new EnumToStringUsingDescription();
            ObservableCollection<KeyValuePair<string, ArTypes>> dateTypeList = new ObservableCollection<KeyValuePair<string, ArTypes>>();

            foreach (ArTypes type in Enum.GetValues(typeof(ArTypes)))
            {
                KeyValuePair<string, ArTypes> typeKeyValue = new KeyValuePair<string, ArTypes>(converter.ConvertTo(null, null, type, typeof(string)).ToString(), type);
                _ArTypeList.Add(typeKeyValue);
            }
        }

        return _ArTypeList;
    }
    set
    {
        _ArTypeList = value;
        OnPropertyChanged("ArTypeList");
    }
}



 private ArTypes _SelectedArType;
        public ArTypes SelectedArType
        {
            get
            {
                return _SelectedArType;
            }
            set
            {
                if (_SelectedArType != value)
                {
                    _SelectedArType = value;
                    OnPropertyChanged("SelectedArType");
                }
            }
        }

TotalAdjustmentsWidget(查看):

private ArTypes _SelectedArType;
    public ArTypes SelectedArType
    {
        get
        {
            return _SelectedArType;
        }
        set
        {
            if (_SelectedArType != value)
            {
                _SelectedArType = value;
            }
        }

    }

TotalAdjustmentsWidgetViewModel:

   public ArTypes SelectedArType
    {
        get
        {
            return this.SettingModel.SelectedArType;
        }
        set
        {
            if (this.SettingModel.SelectedArType != value)
            {
                this.SettingModel.SelectedArType = value;
                OnPropertyChanged("SelectedArType");
            }
        }
    }
//This List is what actually pulls the data for the created widget

        public List<CustomerActivityReport> GetAllCustomerTypeReportsData()
        {
            try
            {
                if (!CanLoad)
                    return null;


                List<CustomerActivityReport> customerTypeReports = dashRepo.GetCustomerActivityReport(1, Convert.ToInt32(SelectedArType), 3, 1, this.Dates.StartDate, 
                                                                                                          this.Dates.EndDate, BeginningRoute, EndingRoute, BeginningAccountNumber,
                                                                                                          EndingAccountNumber, BeginningSequenceNumber, EndingSequenceNumber,
                                                                                                          0, 1, printCustomerTypeTotals, SelectedServiceType.ServiceTypeID,
                                                                                                          SelectedRate.RateID, SelectedCycle.CycleID, -1, SelectedCustomerType.CustomerTypeID);

                return customerTypeReports;
            }
            catch (Exception ex)
            {
                LogException(ex);
            }
            return null; 

        }

ComboBox中填充了正确的值,因此工作正常。 ArTypeList存储值,例如:{“Adjustment”,arAdjustment} 我在TotaAdjustments ViewModels和View中的所有获取/集合中放置了断点。 结果:

  

OptionsViewModel中的SelectedArType最初设置为0   一旦我从组合框中选择了一个项目,那么selectedArType就会被设置为arDepositReceipt(或者我选择的那个)   然后我点击将信息发送到我的Widget的按钮   单击按钮后,断点将转到My TotalAdjustmentsWidgetViewModel中的SelectedArType。
  SelectedArType的值为0而不是arDepositReceipt
  断点然后转到模型中的SelecedArType,它也设置为0.

这会导致SelectedArType的值为0而不是arDepositReceipt。

我有2个ViewModel用于此模型。 TotalAdjustmentsOptionsViewModel供用户设置实际窗口小部件的参数。 TotalAdjustmentsViewModel用于显示与给定参数匹配的数据的实际窗口小部件。

如有任何帮助,请与我们联系。如果您需要更多信息,请与我们联系。

1 个答案:

答案 0 :(得分:0)

好的,所以我仍然在努力完全理解你的设置,但我想我现在有了一个想法。如果这不正确,请告诉我......据我所知,您有一个TotalAdjustmentsViewModel(或TotalAdjustmentsWidgetViewModel取决于您的问题所在的位置)类和{{1 } .class。

我有点希望TotalAdjustmentsOptionsViewModel类是另一个的子视图模型。如果这是真的,那么你有一个很好的简单解决方案来解决你的问题。您可以TotalAdjustmentsOptionsViewModel直接从Bind控件直接转到父视图模型。 (我也假设您在问题中标记为'XAML'的内容实际上来自子视图):

TotalAdjustmentsOptionsView

如果我误解了这种情况,请告诉我。