使用按钮更改绑定的WPF ComboBox SelectedItem

时间:2016-03-03 19:43:10

标签: c# wpf binding combobox command

我有一个付款期限列表,我已经绑定到一个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,它们应该像它们一样开火,但所选项目不会改变。

所有项目都显示正常,当我点击不同的付款期间时,它会加载一个新的时间表,但单击prev和next按钮并不会改变我的SelectedPayPeriod。

我对此很新,所以对任何帮助/批评表示赞赏。

由于

1 个答案:

答案 0 :(得分:0)

当评论说明更新您的约束为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];
    }
}