-1

I have a DatePicker control and also there is a command to load data for specific date. How can I get a value of DatePicker and pass it to command?

My ViewModel class now:

public class MainWindowViewModel
{
    private readonly JiraHandler jiraHandler;
    private DateTime date;

    public ICommand DateChosen { get; }

    public ObservableCollection<Issue> Issues { get; private set; } = new ObservableCollection<Issue>();

    public MainWindowViewModel(JiraHandler jiraHandler)
    {
        this.jiraHandler = jiraHandler;
    }
}
Atlass-
  • 25
  • 5

2 Answers2

0

The xaml part should look like something like this at the end:

        <DatePicker x:Name="MyDatePicker">

        </DatePicker>

        <Button Content="Click me!"
                Command="{Binding Path=DateChosen}"
                CommandParameter="{Binding Path=DisplayDate, ElementName=MyDatePicker}"/>

And in the ViewModel ICommand:

    private ICommand _dateChosen ;

    public ICommand DateChosen 
    {
        get
        {
            if (_dateChosen == null)
            {
                _dateChosen = new RelayCommand(param => OnDateChosen(param));
            }
            return _dateChosen ;
        }
    }

For the implementation of the RelayCommand i can recommend this link: RelayCommand

On the OnDateChosen(object param) method you can cast the 'param' parameter to DateTime.

Good luck!

bazsisz
  • 394
  • 2
  • 11
0

I recommend the community toolkit mvvm. It provides a relaycommand implementation of icommand, messenger, base classes and does code generation.

https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/

Here's a quick sample to give you the idea of how this could work:

    <StackPanel>
        <DatePicker Name="DateSelection"
                    SelectedDate="{Binding DateSelected}"/>
        <Button Content="DoSomething"
                Command="{Binding DateChosenCommand}"
                CommandParameter="{Binding SelectedDate, ElementName=DateSelection}"/>
    </StackPanel>

And the viewmodel:

public partial class MainWindowViewModel : ObservableObject
{

    private readonly JiraHandler jiraHandler;

    [ObservableProperty]
    private DateTime? dateSelected;

    partial void OnDateSelectedChanged(DateTime? newDate)
    {
        // Fires when bound Date value changes
    }

    [RelayCommand]
    private void DateChosen(DateTime? selectedDate)
    {
        if (selectedDate != null)
        {
            dateSelected = (DateTime)selectedDate;
        }
    }

    [ObservableProperty]
    private ObservableCollection<Issue> issues = new ObservableCollection<Issue>();

    public MainWindowViewModel(JiraHandler jiraHandler)
    {
        this.jiraHandler = jiraHandler;
    }

You should use DateSelected from the datepicker. That is a nullable DateTime?

You could potentially bind that selected date and the command references that value.

Notes:

Aways implement inotifypropertychanged on any viewmodel ( to avoid memory leaks. ) The ObservableObject does so. There is also a ObservableValidator which gives validation support.

The public properties are generated in another partial class file.

You automatically get methods you can provide for property changed or propertychanging. This allows you to conveniently act when say the date is selected in your datepicker.

Andy
  • 11,864
  • 2
  • 17
  • 20