[英]SelectedValue in ComboBox always returns 0
I am using the Enum class to allow myself to Convert from Enum to Description easily. 我使用Enum类允许自己轻松地从Enum转换为Description。 I know there are other ways to do this, but this is how my company has done it in the past so I need to stick to the same structure.
我知道还有其他方法可以做到这一点,但这就是我公司过去的做法,因此我需要坚持相同的结构。
Here is my enum class: 这是我的枚举类:
[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: 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: 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(View): TotalAdjustmentsWidget(视图):
private ArTypes _SelectedArType;
public ArTypes SelectedArType
{
get
{
return _SelectedArType;
}
set
{
if (_SelectedArType != value)
{
_SelectedArType = value;
}
}
}
TotalAdjustmentsWidgetViewModel: 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;
}
The ComboBox is populated with the right values, so that is working properly. ComboBox填充了正确的值,因此可以正常工作。 The ArTypeList stores the values for example as: {"Adjustment", arAdjustment} I have put breakpoints at all of the gets/sets in the TotaAdjustments ViewModels and View.
ArTypeList将值存储为例如:{“ Adjustment”,arAdjustment}我已经在TotaAdjustments ViewModels和View中的所有获取/设置处都设置了断点。 Result:
结果:
The SelectedArType in the OptionsViewModel is initially set to 0.
OptionsViewModel中的SelectedArType最初设置为0。
Once I select an item from the combobox the selectedArType is then set to arDepositReceipt(or which ever one I have chosen)一旦我从组合框中选择一项,然后将selectedArType设置为arDepositReceipt(或我选择的那个)
I then click on the button that sends the information to my Widget.然后,我单击将信息发送到我的小部件的按钮。
Once I click the button the breakpoint goes to my SelectedArType in My TotalAdjustmentsWidgetViewModel.单击按钮后,断点将转到我的TotalAdjustmentsWidgetViewModel中的SelectedArType。
The value of SelectedArType is 0 instead of arDepositReceiptSelectedArType的值为0而不是arDepositReceipt
The breakpoint then goes to the SelecedArType in my Model and it is also set to 0.然后将断点转到我的模型中的SelecedArType,并将其也设置为0。
This causes the value for SelectedArType to be 0 instead of arDepositReceipt. 这将导致SelectedArType的值为0而不是arDepositReceipt。
I have 2 ViewModels for this Model. 我为此模型有2个ViewModel。 The TotalAdjustmentsOptionsViewModel is for the user to set the parameters for the actual widget.
TotalAdjustmentsOptionsViewModel供用户设置实际小部件的参数。 The TotalAdjustmentsViewModel is for the actual widget that displays the data that matches the given parameters.
TotalAdjustmentsViewModel适用于实际小部件,该小部件显示与给定参数匹配的数据。
Any help would be appreciated, please let me know if you need any more information. 任何帮助将不胜感激,如果您需要更多信息,请告诉我。
Ok, so I'm still struggling to fully understand your setup but I think I have an idea now. 好的,所以我仍在努力完全理解您的设置,但是我想我现在有了主意。 If this is not correct, please let me know... as far as I can understand, you have a
TotalAdjustmentsViewModel
(or TotalAdjustmentsWidgetViewModel
depending on where in your question you look) class and a TotalAdjustmentsOptionsViewModel
class. 如果这是不正确的,请告诉我...据我所知,您有一个
TotalAdjustmentsViewModel
(或TotalAdjustmentsWidgetViewModel
具体取决于您在问题中的位置)类和TotalAdjustmentsOptionsViewModel
类。
I'm kind of hoping that the TotalAdjustmentsOptionsViewModel
class is a child view model of the other one. 我有点希望
TotalAdjustmentsOptionsViewModel
类是另一个模型的子视图模型。 If that is true, then you have a nice simple solution to your problem. 如果是这样,那么您就可以很好地解决问题。 You could
Bind
directly from the TotalAdjustmentsOptionsView
control directly to the parent view model. 您可以直接从
TotalAdjustmentsOptionsView
控件直接Bind
到父视图模型。 (I'm also assuming that what you labelled as 'XAML' in your question is actually from the child view): (我还假设您在问题中标记为“ XAML”的内容实际上来自子视图):
<telerik:RadComboBox ItemsSource="{Binding ArTypeList}" SelectedValue="{Binding
Path=DataContext.SelectedArType, Mode=TwoWay, ValidatesOnDataErrors=True,
RelativeSource={RelativeSource AncestorType=
{x:Type YourViewsXmlNamespacePrefix:TotalAdjustmentsViewModel}}}" ... />
Please let me know if I have misunderstood the situation. 如果我误解了情况,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.