使用按钮更改绑定的 WPF 组合框选定项
本文关键字:组合 WPF 按钮 绑定 | 更新日期: 2023-09-27 18:37:12
我有一个支付期列表,我已经绑定到一个组合框,我想通过单击按钮(即一组上一个和下一个按钮)循环浏览。根据所选的支付期,我想从我们的数据库中填充时间记录列表。
C# 代码
public TimeSheetViewModel()
{
BuildPayPeriods();
GetEmployeeList();
GetTimes(SelectedEmployee, SelectedPayPeriod);
NextPayPeriod = new RelayCommand(NextPayPeriodCommand);
PrevPayPeriod = new RelayCommand(PrevPayPeriodCommand);
SelectedPayPeriodChanged = new RelayCommand(SelectPayPeriodCommand);
}
public ObservableCollection<PayPeriod> PayPeriods { get; set; }
private PayPeriod _SelectedPayPeriod;
public PayPeriod SelectedPayPeriod
{
get
{
return _SelectedPayPeriod;
}
set
{
_SelectedPayPeriod = value;
GetTimes(SelectedEmployee, value);
OnPropertyChanged("SelectedPayPeriod")
}
}
public RelayCommand NextPayPeriod { get; set; }
void NextPayPeriodCommand(object parameter)
{
int index = PayPeriods.IndexOf((PayPeriod)parameter);
''EDIT
if (index != 0 && CheckForEdits())
{
SelectedPayPeriod = PayPeriods[index - 1];
}
}
public RelayCommand PrevPayPeriod { get; set; }
void PrevPayPeriodCommand(Object parameter)
{
int index = PayPeriods.IndexOf((PayPeriod)parameter);
''EDIT
if (index != (PayPeriods.Count - 1) && CheckForEdits())
{
SelectedPayPeriod = PayPeriods[index + 1];
}
}
public RelayCommand SelectedPayPeriodChanged { get; set; }
void SelectPayPeriodCommand(Object parameter)
{
''EDIT
if (parameter != null && CheckForEdits())
{
int index = PayPeriods.IndexOf((PayPeriod)parameter);
SelectedPayPeriod = PayPeriods[index];
}
}
XAML 代码
<Button x:Name="btnPrevWeek" Command="{Binding PrevPayPeriod}" CommandParameter="{Binding Path=SelectedItem, ElementName=comboPayPeriod}"/>
<ComboBox x:Name="comboPayPeriod" ItemsSource="{Binding PayPeriods}" DisplayMemberPath="Display" SelectedItem="{Binding SelectedPayPeriod, Mode=OneWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding SelectedPayPeriodChanged}" CommandParameter="{Binding Path=SelectedItem, ElementName=comboPayPeriod}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
<Button x:Name="btnNextWeek" Command="{Binding NextPayPeriod}" CommandParameter="{Binding Path=SelectedItem, ElementName=comboPayPeriod}">
我试图省略代码中似乎无关紧要的内容,例如格式和布局内容。
选择支付期更改有效,因此如果我通过单击它手动更改组合框的选定项目,一切正常。另外两个,PrevPayPeriod和NextPayPeriod,像它们应该的那样触发,但所选项目不会改变。
所有项目都显示正常,当我单击不同的付款期时,它会很好地加载新的时间表,但是单击上一个和下一个按钮不会更改我的 SelectedPayPeriod。
我对此相当陌生,因此任何帮助/批评都值得赞赏。
谢谢
随着注释注释将您的绑定更新为 TwoWay
,我也没有看到您那里的触发器有任何附加值。绑定将处理更新:
<Button x:Name="btnPrevWeek" Command="{Binding PrevPayPeriod}" CommandParameter="{Binding Path=SelectedItem, ElementName=comboPayPeriod}"/>
<ComboBox x:Name="comboPayPeriod" ItemsSource="{Binding PayPeriods}" DisplayMemberPath="Display" SelectedItem="{Binding SelectedPayPeriod, Mode=TwoWay}" />
<Button x:Name="btnNextWeek" Command="{Binding NextPayPeriod}" CommandParameter="{Binding Path=SelectedItem, ElementName=comboPayPeriod}">
另外我认为您在上一个和下一个处理程序中的逻辑是相反的,应该是这样的:
void PrevPayPeriodCommand(object parameter)
{
int index = PayPeriods.IndexOf((PayPeriod)parameter);
if (index != 0) // set previous if we're not pointing to the first element
{
SelectedPayPeriod = PayPeriods[index - 1];
}
}
void NextPayPeriodCommand(object parameter)
{
int index = PayPeriods.IndexOf((PayPeriod)parameter);
if (index != PayPeriods.Count - 1) // set next if we're not pointing to last element
{
SelectedPayPeriod = PayPeriods[index + 1];
}
}