Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带有listview.Item的Wpf绑定按钮_C#_Wpf_Data Binding - Fatal编程技术网

C# 带有listview.Item的Wpf绑定按钮

C# 带有listview.Item的Wpf绑定按钮,c#,wpf,data-binding,C#,Wpf,Data Binding,您好,我想将按钮与其他listView.Item绑定。我想要的是像stackoverflow一样的东西。 但我有增加/减少值的问题。我有事件点击,但我不知道如何在列表中获得相应的项目和增加/减少值。 编辑我用了这个按钮。命令,但仍然不起作用。我不知道如何使用这些命令。RoutedEvents不太容易使用DataTemplates,因为您没有可以放置事件代码的代码,您可以使用命令来执行相同的操作。在每个项目的视图模型中(我假设您使用MVVM),创建名为UpVoteCommand和DownVot

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


编辑我用了这个按钮。命令,但仍然不起作用。我不知道如何使用这些命令。

RoutedEvents不太容易使用
DataTemplates
,因为您没有可以放置事件代码的代码,您可以使用命令来执行相同的操作。在每个项目的视图模型中(我假设您使用MVVM),创建名为UpVoteCommand和DownVoteCommand类型的属性非常方便。将它们绑定到属性并删除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命令的实现。

RoutedEvents与
DataTemplates
一起工作不那么容易,因为您没有可以放置事件代码的代码,您可以使用命令来执行相同的操作。在每个项目的视图模型中(我假设您使用MVVM),创建名为UpVoteCommand和DownVoteCommand类型的属性非常方便。将它们绑定到属性并删除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命令的实现。

首先,您需要实现
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); }
}
<Button Content="+" Command="{Binding Path=UpVoteCmd}"/>
<TextBlock Text="{Binding Path=Feedback}"/>            
<Button Content="-" Command="{Binding Path=DownVoteCmd}"/>
然后在XAML中绑定新命令,如下所示:

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); }
}
<Button Content="+" Command="{Binding Path=UpVoteCmd}"/>
<TextBlock Text="{Binding Path=Feedback}"/>            
<Button Content="-" Command="{Binding Path=DownVoteCmd}"/>

首先,您需要实现
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); }
}
<Button Content="+" Command="{Binding Path=UpVoteCmd}"/>
<TextBlock Text="{Binding Path=Feedback}"/>            
<Button Content="-" Command="{Binding Path=DownVoteCmd}"/>
然后在XAML中绑定新命令,如下所示:

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); }
}
<Button Content="+" Command="{Binding Path=UpVoteCmd}"/>
<TextBlock Text="{Binding Path=Feedback}"/>            
<Button Content="-" Command="{Binding Path=DownVoteCmd}"/>


以同样的方式绑定
反馈
绑定
按钮。命令
到项目中的
向上投票
向下投票
命令并增加/减少
反馈
确定我将尝试thx。我不知道命令属性。您是否使用了一些MVVM框架或实现了
ICommand
?不知道。我正在学习wpf中的工作原理。我很想学习MVVM,但是在我稍微了解了wpf之后。你的onClick代码是什么样子的,当我们在学习时,你设置了数据上下文吗?用同样的方法绑定
反馈
绑定
按钮。命令
到项目中的
向上投票
向下投票
命令并增加/减少
反馈
确定我将尝试thx。我不知道命令属性。您是否使用了一些MVVM框架或实现了
ICommand
?不知道。我正在学习wpf中的工作原理。我很想学习MVVM,但是在我稍微了解了wpf之后。你的onClick代码是什么样子的,在我们学习时,你设置了datacontext吗?我这么做了,但我不知道如何使用ICommand增加或减少值。我尝试了getter setter put+-1,但没有用。@Luffy检查我的答案,我添加了一个小代码示例。简而言之,您对该类的编辑表明您从未将ICommand初始化为实际实例。我这样做了,但我不知道如何使用ICommand增加或减少值。我尝试了getter setter put+-1,但没有用。@Luffy检查我的答案,我添加了一个小代码示例。简而言之,您对该类的编辑表明您从未将ICommand初始化为实际实例。