我正在开发一个应用程序,我使用模板10和MVVM模式进行数据处理,无论如何
我有一个DatePicker和一个组合框,当我通过构造函数设置组合框或导航到页面事件时,组合框被设置并显示值已设置。
我的组合框用于设置两周(24种不同的选择)所以我想添加事件,每当我更改datepicker事件时,我会自动设置组合框中的两周,显示相应于所选日期的正确的两周。
但是当我设置一个新的日期时,我的组合框变空或者空值,什么也没显示,我的转换器将我的枚举转换为int值,但它没有显示在我的组合框中这是我的xaml for my控制:
<DatePicker x:Name="PaymentDateSeniorityInfoDatePicker"
Date="{Binding PaymentDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource DateToDatePickerConverter}}"
DateChanged="{x:Bind ViewModel.PaymentDateChanged}"
IsEnabled="{x:Bind ViewModel.ControlEnabled, Mode=OneWay, FallbackValue='true'}"/>
<ComboBox x:Name="FortnightNumberSeniorityBonusInfoComboBox"
ItemsSource="{x:Bind ViewModel.Fortnights, Mode=OneWay}"
SelectedValuePath="Value"
DisplayMemberPath="Display"
SelectedValue="{Binding FortnightNumber, Mode=TwoWay, Converter={StaticResource EnumItemsConverter}}"
SelectedIndex="{Binding FortnightNumber, Mode=OneWay, Converter={StaticResource EnumItemsConverter}}"
IsEnabled="{x:Bind ViewModel.ControlEnabled, Mode=OneWay}"/>
当我导航到页面时,这是设置我的组合框的逻辑事件
public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary<string, object> state)
{
ShowAndHideButtons();
AreAdsCollapsed = AdsCollapsed();
LoadSeniorityBonusInfo();//This method set my fortnights
await ExecutedRefreshedAsync();
await base.OnNavigatedToAsync(parameter, mode, state);
}
void LoadSeniorityBonusInfo()
{
SeniorityBonusInfo.FortnightNumber = GetFortnight(SeniorityBonusInfo.PaymentDate.Value);
SeniorityBonusInfo.DaysOfSalary = GetDaysOfSalary(LaborInfo, PersonalInfo);
SeniorityBonusInfo.SeniorityBonusPayment = CalculateSenorityBonus(SeniorityBonusInfo.DaysOfSalary, SeniorityBonusInfo.CurrentBasicSalary);
}
这是设置我新的两周的事件:
public void PaymentDateChanged()
{
SeniorityBonusInfo.FortnightNumber = GetFortnight(SeniorityBonusInfo.PaymentDate.Value);
}
最后这是我的枚举转换器:
public class EnumItemsConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (!value.GetType().GetTypeInfo().IsEnum)
return false;
var enumName = value.GetType();
var obj = Enum.Parse(enumName, value.ToString());
return System.Convert.ToInt32(obj);
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
var enumType = (value as Enum).GetType();
return Enum.ToObject(enumType, System.Convert.ToInt32(value));
}
}
有人可以帮我解决这个问题,在设置另一个控件时,为了更新控件(例如组合框),我忘记了什么或者让它跳过。
它可以在没有问题的情况下使用textblock,但在这种情况下它无法正常工作。
我感谢一些指导,谢谢!
更新
我正在添加一位老乡问我的课程。
ValidatableModelBase是一个来自Template10的类。由Jerry Nixon开发的验证库是一个github项目,你可以通过github将它添加到你的项目中。
至于BonusDays和Fortnight只是枚举,这是我的枚举:
public enum BonusDays : Int32
{
[Display(Name = "EnumBonusDays0")]
BonusDays0 = 0,
[Display(Name = "EnumBonusDays18")]
BonusDays18 = 1,
[Display(Name = "EnumBonusDays33")]
BonusDays33 = 2,
[Display(Name = "EnumBonusDays48")]
BonusDays48 = 3,
[Display(Name = "EnumBonusDays63")]
BonusDays63 = 4,
[Display(Name = "EnumBonusDays93")]
BonusDays93 = 5,
[Display(Name = "EnumBonusDays108")]
BonusDays108 = 6
}
public enum Fortnight : Int32
{
[Display(Name = "EnumFortnightQ00")]
Q00 = 0,
[Display(Name = "EnumFortnightQ01")]
Q01 = 1,
[Display(Name = "EnumFortnightQ02")]
Q02 = 2,
[Display(Name = "EnumFortnightQ03")]
Q03 = 3,
[Display(Name = "EnumFortnightQ04")]
Q04 = 4,
[Display(Name = "EnumFortnightQ05")]
Q05 = 5,
[Display(Name = "EnumFortnightQ06")]
Q06 = 6,
[Display(Name = "EnumFortnightQ07")]
Q07 = 7,
[Display(Name = "EnumFortnightQ08")]
Q08 = 8,
[Display(Name = "EnumFortnightQ09")]
Q09 = 9,
[Display(Name = "EnumFortnightQ10")]
Q10 = 10,
[Display(Name = "EnumFortnightQ11")]
Q11 = 11,
[Display(Name = "EnumFortnightQ12")]
Q12 = 12,
[Display(Name = "EnumFortnightQ13")]
Q13 = 13,
[Display(Name = "EnumFortnightQ14")]
Q14 = 14,
[Display(Name = "EnumFortnightQ15")]
Q15 = 15,
[Display(Name = "EnumFortnightQ16")]
Q16 = 16,
[Display(Name = "EnumFortnightQ17")]
Q17 = 17,
[Display(Name = "EnumFortnightQ18")]
Q18 = 18,
[Display(Name = "EnumFortnightQ19")]
Q19 = 19,
[Display(Name = "EnumFortnightQ20")]
Q20 = 20,
[Display(Name = "EnumFortnightQ21")]
Q21 = 21,
[Display(Name = "EnumFortnightQ22")]
Q22 = 22,
[Display(Name = "EnumFortnightQ23")]
Q23 = 23,
[Display(Name = "EnumFortnightQ24")]
Q24 = 24
}
我使用Display atribute来显示从英语到西班牙语的显示值,反之亦然!!!
这是我的重要人物;可以帮助我进行本地化:
public class ValueDisplay
{
public Enum Value { get; set; }
public string Display { get; set; }
}
这是我的枚举扩展,以帮助我进行本地化:
public static class EnumExtensions
{
public static string Display(this Enum eValue)
{
var nAttributes = eValue.GetType().GetField(eValue.ToString()).GetCustomAttributes(typeof(DisplayAttribute), false);
var DisplayName = string.Empty;
if (nAttributes.Any())
{
DisplayName = GetLocalizeString((nAttributes.FirstOrDefault() as DisplayAttribute).Name);
return DisplayName;
}
else
{
throw new ArgumentException("No hay Attributo asignado para el Enum");
}
}
}
最后这是我的本地化字符串类
public static class LocalizeService
{
public static string GetLocalizeString(string varName)
{
return App.Loader.GetString(varName);
}
}
希望这能为您提供足够的信息,感谢您的关注!
答案 0 :(得分:0)
我解决了这个问题,但是我不得不使用常规事件并留下我的viewmodel x:bind方法。我只是在Datephanged事件的datepicker上放了一个事件,并在后面的代码中设置了组合框索引。这是我的xaml和代码背后的代码。
我不喜欢这个解决方案,但是因为我不想在这里停下来,所以当我发现如何使用MVVM模式完全解决它时,它会工作。
希望有人比我有更好的解决方案。这是我的代码:
XAML:
<DatePicker x:Name="PaymentDateSeniorityInfoDatePicker"
Date="{Binding PaymentDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource DateToDatePickerConverter}}"
DateChanged="PaymentDateSeniorityInfoDatePicker_DateChanged"
IsEnabled="{x:Bind ViewModel.ControlEnabled, Mode=OneWay, FallbackValue='true'}"/>
视图模型:
public void PaymentDateChanged()
{
SeniorityBonusInfo.FortnightNumber = GetFortnight(SeniorityBonusInfo.PaymentDate.Value);
}
public SeniorityBonusDTO SeniorityBonusInfo
{
get { return _SeniorityBonusInfo; }
set { Set(ref _SeniorityBonusInfo, value); }
}
我的DTO模型:
public class SeniorityBonusDTO : ValidatableModelBase
{
[Required]
public DateTime? PaymentDate
{
get { return Read<DateTime?>(); }
set { Write(value, nameof(PaymentDate), false); }
}
[Required]
public decimal? CurrentBasicSalary
{
get { return Read<decimal?>(); }
set { Write(value, nameof(CurrentBasicSalary), false); }
}
[Required]
public BonusDays DaysOfSalary
{
get { return Read<BonusDays>(); }
set { Write(value, nameof(DaysOfSalary), false); }
}
[Required]
public Fortnight FortnightNumber
{
get { return Read<Fortnight>(); }
set { Write(value, nameof(FortnightNumber), false); }
}
[Required]
public decimal? SeniorityBonusPayment
{
get { return Read<decimal?>(); }
set { Write(value, nameof(SeniorityBonusPayment), false); }
}
}
我的代码背后:
private void PaymentDateSeniorityInfoDatePicker_DateChanged(object sender, DatePickerValueChangedEventArgs e)
{
ViewModel.PaymentDateChanged();
FortnightNumberSeniorityBonusInfoComboBox.SelectedIndex = (int)ViewModel.SeniorityBonusInfo.FortnightNumber;
}
如果有人有更好的想法,我将非常感谢你的帮助,谢谢!