【发布时间】:2013-12-01 21:51:38
【问题描述】:
我正在使用 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(或我选择的任何一项)
然后我单击将信息发送到我的小部件的按钮。
单击按钮后,断点将转到 My TotalAdjustmentsWidgetViewModel 中的 SelectedArType。
SelectedArType 的值为 0 而不是 arDepositReceipt
然后断点转到我的模型中的 SelecedArType,它也设置为 0。
这会导致 SelectedArType 的值为 0 而不是 arDepositReceipt。
我有 2 个用于此模型的 ViewModel。 TotalAdjustmentsOptionsViewModel 供用户设置实际小部件的参数。 TotalAdjustmentsViewModel 用于显示与给定参数匹配的数据的实际小部件。
任何帮助将不胜感激,如果您需要更多信息,请告诉我。
【问题讨论】:
-
对,我在两个视图模型中看到了一个
SelectedArType属性...GetAllCustomerTypeReportsData方法中该属性的值是多少?那是从Button.Click调用的那个吗? -
是的,该方法是通过单击按钮调用的,此时它也设置为 0。
-
长话短说,SelectedArType 被设置在 OptionsViewModel 中,而不是另一个。
-
所以
OptionsViewModel中的实际ComboBox部分工作正常吗?在我看来,您在一个视图模型中正确设置了属性,然后尝试在您的方法中使用另一个视图模型中的属性。如果这是正确的,请编辑您的问题,添加两个视图模型之间的关系。不幸的是,我很快就会消失几个小时,但我会在回来时尝试看看。
标签: c# wpf mvvm combobox enums