如何用另一个控件(例如按钮)更改wpf组合框选择?

时间:2013-08-29 19:36:30

标签: wpf xaml mvvm

我在同一页面上有一个组合框和两个按钮。按钮是“上一个”和“下一个”。理想情况下,我想使用按钮相应地选择组合框项目。此外,我希望在到达列表的结尾或开始时禁用“下一步”和“上一步”按钮。我不认为在代码隐藏中这样做会有问题,但我想知道这是否可以通过xaml / binding / mvvm实现。谢谢。

3 个答案:

答案 0 :(得分:0)

是的,这是完全可能的。首先,我强烈建议you use an MVVM framework,其次使用Caliburn.Micro这样的框架,您可以在视图中从Button轻松调用视图模型上的动词(方法)。

因此,例如,您的视图中会有<Button x:Name="Next">Next</Button>,视图模型中会有相应的public void Next()方法。 Caliburn.Micro将处理从Button点击事件到具有相同名称的视图模型方法的基于约定的约束。

Next方法中,您将增加当前页面。这也是您的视图模型上的公共属性,它实现INotifyPropertyChanged。诸如Caliburn.Micro之类的MVVM框架提供了一种方法,该方法将使用lambda表达式而不是魔术属性名称字符串来调用PropertyChanged事件。

然后,在您的视图中,您可以将ComboBox ItemsSource绑定到可能的页码集合,将SelectedItem绑定到当前页面的公共属性。

听起来你正在做分页,你可能还想考虑PagedList库(UI不可知)和相应的NuGet包。

答案 1 :(得分:0)

快速而肮脏的实施。

cs:

public partial class MainWindow : Window , INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }

    private List<string> _items;
    public List<string> Items 
    { 
        get
        {
            if (_items == null)
                _items = new List<string> { "1", "2", "3", "4", "5" };
            return _items;
        }
    }

    private string _selectedItem;
    public string SelectedItem
    {
        get { return _selectedItem; }
        set
        {
            _selectedItem = value;
            if(PropertyChanged!= null)
                PropertyChanged(this, new PropertyChangedEventArgs("SelectedItem"));
        }

    }

    private ICommand _nextCommand;
    public ICommand NextCommand
    { 
        get
        {
            if (_nextCommand == null)
                _nextCommand = new RelayCommand<string>(MoveToNext, CanMoveToNext);
            return _nextCommand;                    
        }
    }

    private ICommand _previousCommand;
    public ICommand PreviousCommand
    {
        get
        {
            if (_previousCommand == null)
                _previousCommand = new RelayCommand<string>(MoveToPrevious, CanMoveToPrevious);
            return _previousCommand;
        }
    }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    private void MoveToNext(object currentItem)
    {
        var currentIndex = _items.IndexOf((string)currentItem);
        SelectedItem = _items.ElementAt(currentIndex + 1);
    }

    private bool CanMoveToNext(object currentItem)
    {
        return currentItem != null && _items.Last<string>() != (string)currentItem;
    }

    private void MoveToPrevious(object currentItem)
    {
        var currentIndex = _items.IndexOf((string)currentItem);
        SelectedItem = _items.ElementAt(currentIndex - 1);
    }

    private bool CanMoveToPrevious(object currentItem)
    {
        return currentItem != null && _items.First<string>() != (string)currentItem;
    }
}

RelayCommand

XAML:

<Grid>        
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />            
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>


    <Button Content="Next" Command="{Binding NextCommand}"  CommandParameter="{Binding ElementName=box, Path=SelectedItem}"/>
    <Button Content="Prev" Grid.Column="1" Command="{Binding PreviousCommand}"  CommandParameter="{Binding ElementName=box, Path=SelectedItem}"/>

    <ComboBox x:Name="box" 
              Grid.Row="1" Grid.ColumnSpan="2"
              ItemsSource="{Binding Items}"  
              SelectedItem="{Binding SelectedItem}"
              />                        
</Grid>  

答案 2 :(得分:0)

我建议采用以下方法:

  • Regular Combobox
  • 带有Enabled属性的按钮(上一页和下一页)使用转换器绑定到ComboBox SelectedIndex

然后,您可以使用常规按钮事件或thorugh命令

设置新索引