![](/img/trans.png)
[英]Weird behavior for a property bound to the 'SelectedItem' of a WPF Combobox
[英]Changing a bound WPF ComboBox SelectedItem with a button
我有一個綁定到要通過單擊按鈕(即一組上一個和下一個按鈕)循環通過的ComboBox的“付款期限”列表。 基於選定的支付期,我想從數據庫中填充一個時間記錄列表。
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}">
我試圖省略似乎無關的代碼中的內容,例如格式和布局內容。
SelectedPayPeriodChanged起作用,因此,如果我通過單擊它來手動更改組合框的選定項,則一切正常。 另外兩個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];
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.