Hello!!! It's simple. There is a control in WPF-Calendar. You must select multiple dates from the calendar. and vice versa, mark the dates added to the list in the calendar.

Those. two-sided binding on SelectedDates. But as in many controls for collections (ListView, ListBox), there is no normal selection for multiple selection. Using the event selection element you can create a list of selected dates, but here's how to highlight dates in Calendar?

<Calendar Name="TournamentCalendar" Grid.Column="5" FirstDayOfWeek="Monday" SelectionMode="MultipleRange" SelectedDates="{Binding Path=SelectedDateTimes}"> <i:Interaction.Triggers> <i:EventTrigger EventName="SelectedDatesChanged"> <cal:ActionMessage MethodName="AssignDates"> <cal:Parameter Value="{Binding SelectedDates, ElementName=TournamentCalendar}"/> </cal:ActionMessage> </i:EventTrigger> </i:Interaction.Triggers> </Calendar> 

in viewmodel

  public void AssignDates(SelectedDatesCollection dates) { //Добавляем дату } 

Maybe something else do? Tried to search for third-party controls but all paid (((, in particular WPFExtensions.

    3 answers 3

    Your case is one example of the fact that sometimes it is necessary to call a method from codebooks, i.e. establishing a View <- ViewModel.

    Directly above public partial class MainWindow we will write the interface

     public interface ICodeBehind { void SelectManyDates(List<DateTime> manyDates); } 

    Inherit the MainWindow from this interface, and implement it.

     public void SelectManyDates(List<DateTime> manyDates) { SelectedDatesCollection theDates = Calendar1.SelectedDates; //в цикле foreach пробегаем по manyDates и добавляем в theDates } 

    Let's go to the ViewModel and create a property to associate with codebook.

      /// <summary> /// Ссылка на MainWindow.xaml.cs /// </summary> public ICodeBehind MainWindowCodeBehind { get; set; } 

    Let's go back to codebook and tie this property either in the MainWindow constructor or in the Load event.

     //ссылыка на MainViewModel ((MainViewModel)DataContext).MainWindowCodeBehind = this; 

    Now in ViewModel, you can call

     MainWindowCodeBehind.SelectManyDates(dates); 
    • I will try !! generally understood. It is strange that such simple things are not implemented out of the box. - Aldmi
    • I have ((AppViewModel) DataContext) .AppWindowCodeBehind = this; dataContext == NULL. See my answer above. - Aldmi

    EVERYTHING IS WORKING. You need to bind in the OnActivated method.

      protected override void OnActivated(EventArgs e) { ((AppViewModel)DataContext).AppWindowCodeBehind = this; base.OnActivated(e); } 
    • Very glad what happened :) - Bulson
     /// <summary> /// Interaction logic for AppView.xaml /// </summary> public partial class AppView : Window, ICodeBehind { public AppView() { InitializeComponent(); ((AppViewModel)DataContext).AppWindowCodeBehind = this; } public void SelectManyDates(BindableCollection<DateTime> manyDates) { SelectedDatesCollection theDates = Calendar.SelectedDates; //в цикле foreach пробегаем по manyDates и добавляем в theDates foreach (var date in manyDates) { theDates.Add(date); } } } 

    DataContext is NULL in the constructor. I use Caliburn.Micro . Caliburn.Micro . What's wrong?

    • I described the case when the ViewModel is connected to the View like this: <Window.DataContext> <local:MainViewModel /> </Window.DataContext> I do not use frameworks. Look for how your framework connects to View View the desired ViewModel. Maybe through ViewModelLocator. Look in XAML if the connection goes to a DataContext of some kind of Grid or Panel, then you have to rewrite the string in the constructor. And so it works, tested on several projects. - Bulson 5:56