【问题标题】:Wpf binding button with listview.Item带有 listview.Item 的 Wpf 绑定按钮
【发布时间】:2013-07-01 02:33:25
【问题描述】:

您好,我想将按钮与其他 listView.Item 绑定。我想要的是像我们在stackoverflow上拥有的东西。 但我有增加/减少价值的问题。我有事件 Click 但我不知道如何在列表中获取相应的项目并增加/减少值。

<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;
        }
    }
}

编辑我使用了这个按钮。命令但它仍然无法正常工作。我不知道如何处理这些命令。

【问题讨论】:

  • 以与您在项目中绑定feedback 绑定Button.CommandupVotedownVote 命令相同的方式,并在那里增加/减少feedback
  • 好的,我会试试的。我不知道 Command 属性。
  • 您是否使用了一些 MVVM 框架或实现了ICommand
  • 不。我正在学习 wpf 中的工作原理。我想学习MVVM,但是在我对wpf有点了解之后。
  • 你的 onClick 代码是什么样子的,当我们这样做的时候,你设置了 datacontext 吗?

标签: c# wpf data-binding


【解决方案1】:

RoutedEvents 与DataTemplates 一起工作并不容易,因为您没有可以放置事件代码的代码。 While there are ways to do that,你可以使用命令来做同样的事情。在每个项目的视图模型中(我只是假设您使用 MVVM)创建名为 UpVoteCommand 和 DownVoteCommand 类型的属性 ICommandDelegateCommands 非常方便。将它们绑定到 Command 属性并删除 DataTemplate 中的 Click 处理程序。

[编辑]

列表中一个条目的可能视图模型的小示例,可以被赞成或反对。

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 命令。

【讨论】:

  • 我这样做了,但我不知道如何使用 ICommand 增加减少值。我尝试在 getter setter put +-1 中,但我不工作。
  • @Luffy 检查我的答案,我添加了一个小代码示例。简而言之,您对类的编辑表明您从未将 ICommand 初始化为真实实例。
【解决方案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}"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-10
    • 2016-01-15
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    • 2019-03-04
    • 2015-03-15
    相关资源
    最近更新 更多