带有listview.Item的Wpf绑定按钮

时间:2013-07-03 11:59:39

标签: c# wpf data-binding

您好我想将按钮与其他listView.Item绑定。我想要的是拥有像stackoverflow一样的东西。 但我有增加/减少价值的问题。我有事件点击,但我不知道如何获得列表上的相应项目和增加/减少值。 List view. Concept model

<DataTemplate>
    <StackPanel Orientation="Vertical">
        <StackPanel Orientation="Horizontal">
            <Label Width="706" Height="75" Content="{Binding feedback}"/>
            <StackPanel Orientation="Vertical">
                <Button Name="buttonUp" Content="^" Command="{Binding upVoteCommand}" />
                <Label HorizontalContentAlignment="Center" Width="50" Content="{Binding grade}"/>
                <Button Name="buttonDown" Content="v" Command="{Binding upVoteCommand}"/>
            </StackPanel>
        </StackPanel>
        <Label>-</Label>
    </StackPanel >

修改

class A {
    public string feedback {
        get;
        set;
    }
    public int grade {
        get;
        set;
    }

    private ICommand _upVoteCommand;
    private ICommand _downVoteCommand;
    public ICommand upVoteCommand {
        get {
            return _upVoteCommand;
        }
        set {
            _upVoteCommand = value;
        }
    }
    public ICommand downVoteCommand {
        get {
            return _downVoteCommand;
        }
        set {
            _downVoteCommand = value;
        }
    }
}
编辑我使用了这个按钮。但仍然无法正常工作。我不知道如何处理这些命令。

2 个答案:

答案 0 :(得分:2)

首先,您需要实现ICommand,这样您就可以将命令从视图模型绑定到控件,如下所示:

public class RelayCommand : ICommand
{
  private readonly Action<object> _execute;
  private readonly Predicate<object> _canExecute;

  public RelayCommand(Action<object> execute) : this(execute, null) { }

  public RelayCommand(Action<object> execute, Predicate<object> canExecute)
  {
      if (execute == null) throw new ArgumentNullException("execute");
      _execute = execute; 
      _canExecute = canExecute;
  }

  public bool CanExecute(object parameter)
  {
      return _canExecute == null ? true : _canExecute(parameter);
  }

  public event EventHandler CanExecuteChanged
  {
      add { CommandManager.RequerySuggested += value; }
      remove { CommandManager.RequerySuggested -= value; }
  }

  public void Execute(object parameter) { _execute(parameter); }
}

然后在您发布Feedback的班级中,您需要发布2个新的RelayCommand进行上/下投票,相应地修改Feedback属性。您可以在下面找到我用于测试的课程:

public class MyClass : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;

  private void OnPropertyChanged(string propertyName)
  {
      if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  }

  private int _feedback = 0;

  public int Feedback
  {
      get { return _feedback; }
      set
      {
          if (_feedback == value) return;
          _feedback = value;
          OnPropertyChanged("Feedback");
      }
  }

  private RelayCommand _upVoteCmd;

  public ICommand UpVoteCmd
  {
      get
      {
          if (_upVoteCmd == null) _upVoteCmd = new RelayCommand(o => Feedback += 1);
          return _upVoteCmd;
      }
  }

  private RelayCommand _downVoteCmd;

  public ICommand DownVoteCmd
  {
      get
      {
          if (_downVoteCmd == null) _downVoteCmd = new RelayCommand(o => Feedback -= 1);
          return _downVoteCmd;
      }
  }

}

然后在XAML中绑定新命令,如下所示:

<Button Content="+" Command="{Binding Path=UpVoteCmd}"/>
<TextBlock Text="{Binding Path=Feedback}"/>            
<Button Content="-" Command="{Binding Path=DownVoteCmd}"/>

答案 1 :(得分:1)

使用DataTemplates时,RoutedEvents无法轻松工作,因为您的事件代码可以放置在后面。 While there are ways to do that,你可以使用命令来做同样的事情。在每个项目的视图模型中(我假设您使用MVVM)创建名为UpVoteCommand的属性和ICommand类型的DownVoteCommand,DelegateCommands对此非常方便。将它们绑定到Command属性并删除DataTemplate中的Click处理程序。

[编辑]

列表中一个条目的可能Viewmodel的小例子,可以向上或向下投票。

class MyEntryViewModel : INotifyPropertyChanged
{
    public MyEntryViewModel()
    {
        UpVoteCommand = new DelegateCommand(OnUpVoteCommand);
    }
    public int Votes 
    {
        get {return mVotes;}
        set {mVotes = value; RaiseProperty("Votes");}
    }

    public ICommand UpVoteCommand 
    {
        get; private set;
    }

    void OnUpVoteCommand(object aParameter)
    {
        Votes++;
    }
}

为了简单起见,我离开了INotifyPropertyChanged和down vote命令的实现。