为什么在触发另一个控件事件时无法更新 ComboBox 显示?
Why cant I update ComboBox display when triggering another control event?
我正在开发一个应用程序,我正在使用模板 10 和 MVVM 模式进行数据编制,无论如何
我有一个 DatePicker 和一个组合框,每当我通过构造函数或导航到页面事件设置组合框时,组合框都会设置并显示值已设置。
我的组合框用于设置两周(24 种不同的选择)所以我想添加一个事件,每当我更改日期选择器事件时,我都会自动在组合框中设置两周,根据所选日期显示正确的两周。
但是每当我设置一个新日期时,我的组合框就会变成空白或空值,什么都不显示,我的转换器会注册我的枚举到 int 值的转换,但它不会显示在我的组合框中这是我的 xaml 用于我的控件:
<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));
}
}
谁能帮我解决这个问题,我在设置另一个控件时为了更新组合框等控件而忘记或跳过了什么。
它可以毫无问题地使用文本块,但在这种情况下它不起作用。
非常感谢您的指导!!!
更新
我正在添加一个人问我的 classes。
ValidatableModelBase 是 Jerry Nixon 开发的 Template10.Validation 库中的 class 是一个 github 项目,您可以通过 github.[=18 将其添加到您的项目中=]
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
}
我正在使用显示属性将显示值从英语本地化为西班牙语,反之亦然!!!
这是我的价值displ;ay class 帮助我进行本地化:
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");
}
}
}
最后这是我的本地化字符串 class
public static class LocalizeService
{
public static string GetLocalizeString(string varName)
{
return App.Loader.GetString(varName);
}
}
希望这能为您提供足够的信息,感谢您的关注!
我解决了它,但我不得不使用常规事件并留下我的 viewmodel x:bind 方法。我只是在 DateChanged 事件的 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;
}
如果有人有更好的主意,我将不胜感激,谢谢!!!
我正在开发一个应用程序,我正在使用模板 10 和 MVVM 模式进行数据编制,无论如何
我有一个 DatePicker 和一个组合框,每当我通过构造函数或导航到页面事件设置组合框时,组合框都会设置并显示值已设置。
我的组合框用于设置两周(24 种不同的选择)所以我想添加一个事件,每当我更改日期选择器事件时,我都会自动在组合框中设置两周,根据所选日期显示正确的两周。
但是每当我设置一个新日期时,我的组合框就会变成空白或空值,什么都不显示,我的转换器会注册我的枚举到 int 值的转换,但它不会显示在我的组合框中这是我的 xaml 用于我的控件:
<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));
}
}
谁能帮我解决这个问题,我在设置另一个控件时为了更新组合框等控件而忘记或跳过了什么。
它可以毫无问题地使用文本块,但在这种情况下它不起作用。
非常感谢您的指导!!!
更新
我正在添加一个人问我的 classes。
ValidatableModelBase 是 Jerry Nixon 开发的 Template10.Validation 库中的 class 是一个 github 项目,您可以通过 github.[=18 将其添加到您的项目中=]
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
}
我正在使用显示属性将显示值从英语本地化为西班牙语,反之亦然!!!
这是我的价值displ;ay class 帮助我进行本地化:
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");
}
}
}
最后这是我的本地化字符串 class
public static class LocalizeService
{
public static string GetLocalizeString(string varName)
{
return App.Loader.GetString(varName);
}
}
希望这能为您提供足够的信息,感谢您的关注!
我解决了它,但我不得不使用常规事件并留下我的 viewmodel x:bind 方法。我只是在 DateChanged 事件的 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;
}
如果有人有更好的主意,我将不胜感激,谢谢!!!