使用 INotifyPropertyChanged 如何单击 wpf 按钮

using INotifyPropertyChanged how to Click wpf button

我需要在日期更改时更新 wpf 列表视图。我已经编写了单击按钮时更新列表视图的方法。使用 INotifyPropertyChanged 在日期更改时触发相同的按钮。我有以下代码,但需要帮助才能单击相同的按钮或任何其他方法。

这是需要点击的wpf按钮。 <Button x:Name="SearchButton" Content="Search" HorizontalAlignment="Left" Margin="344,161,0,0" VerticalAlignment="Top" Click="SearchButton_Click" FontSize="18" FontWeight="Bold" />

 public MainPage()
    {
        this.InitializeComponent();
        this.DataContext = clock;
        clock.InitClock(); //using another INotifyPropertyChanged to update clock on textbox
        cDate.dateInitClock();
}

public class ChangeDate : INotifyPropertyChanged
{
    DispatcherTimer dateTimer = new DispatcherTimer();

    public string _date { get; set; }
    public string Date
    {
        get { return _date; }
        set
        {
            _date = value;
            OnPropertyChanged("Date");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

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

    public void dateInitClock()
    {
        dateTimer.Tick += dateTimer_Tick;
        dateTimer.Interval = new TimeSpan(0, 0, 1, 0);
        dateTimer.Start();
    }

    private void dateTimer_Tick(object sender, object e)
    {
        // do we need to add action here?
    }

}

上次编辑:Here link 更新 ListView 中列的工作解决方案 (计时器每秒增加 1 分钟)


首先为你的中继命令创建一个class

using System;
using System.Diagnostics;
using System.Windows.Input;

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

        #region Constructors

        /// <summary>
        /// Creates a new command.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        /// <param name="canExecute">The execution status logic.</param>
        public RelayCommand(Action<object> execute, Predicate<object> canExecute = null)
        {
            _execute = execute ?? throw new ArgumentNullException(nameof(execute));
            _canExecute = canExecute;
        }
        #endregion // Constructors

        #region ICommand Members
        [DebuggerStepThrough]
        public bool CanExecute(object parameter)
        {
            //For Vs 2015+ 
            return _canExecute?.Invoke(parameter) ?? true;
            //For Vs 2013-
            return _canExecute != null ? _canExecute.Invoke(parameter) : true;
        }

        /// <summary>
        /// Can execute changed event handler.
        /// </summary>
        public event EventHandler CanExecuteChanged
        {
            add => CommandManager.RequerySuggested += value;
            remove => CommandManager.RequerySuggested -= value;
        }

        public void Execute(object parameter)
        {
            _execute(parameter);
        }
        #endregion // ICommand Members
    }

然后您可以直接从您的 ViewModel

执行您的命令
//with some validation to enable/disable button click

public virtual ICommand SearchCommand => new RelayCommand(o=> Search(), o=> Validation());

//without validation

 public virtual ICommand SearchCommand => new RelayCommand(o=> Search());

private void Search()
{

}

private bool Validation()
{

}

//using parametres
public virtual ICommand SearchCommand => new RelayCommand(Search);
private void Search(object parameter)
{

}

你的XAML

<Button Content="Search"  Command={Binding SearchCommand}  />

使用 MVVM

ListView 绑定示例
<ListView ItemsSource="{Binding Path=YourData}">
    <ListView.View>
         <GridView>
             <GridViewColumn Header="ID" Width="Auto" 
                  DisplayMemberBinding="{Binding ID}" >
             </GridViewColumn>
             <GridViewColumn DisplayMemberBinding="{Binding Name}" 
                  Header="Name" Width="100"/>
             <GridViewColumn DisplayMemberBinding="{Binding Price}" 
                  Header="Price" Width="100"/>
             <GridViewColumn DisplayMemberBinding="{Binding Author}" 
                  Header="Author" Width="100"/>
             <GridViewColumn DisplayMemberBinding="{Binding Catalog}" 
                  Header="Catalog" Width="100"/>
             <GridViewColumn>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                      <Button Content="Search"  Command={Binding SearchCommand}  CommandParameter="{Binding Id}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
             </GridViewColumn>

           </GridView>
    </ListView.View>
</ListView>

你的视图模型

public class MyViewModel: INotifyPropertyChanged
{
    //INotifyPropertyChanged implementation

    public ObservableCollection<MyData> MyData 
    {
        get { return _myData }
        set
        {
             _myData = value;
             OnPropertiChange(nameof(MyData));
        }
    }

    public virtual ICommand SearchCommand => new RelayCommand(o=> Search());

    private void Search()
    {
        //DoSamething

        MyData = new ObservableCollection<MyData>(ListOfMyData);
    }

}