在组件外部单击后,日历鼠标左按钮向下被触发

本文关键字:按钮 鼠标 外部 组件 单击 日历 | 更新日期: 2023-09-27 18:21:32

我想通过单击日历控件中的日期,使用MouseLeftButtonDown引发事件。但是,在单击该组件外部之前,它不会被提起。

这是xaml代码:

<Controls:BasicCalendar Grid.Row="0"
                        x:Name="DemoCalendar" 
                        DisplayDate="{Binding Path=DisplayDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                        HighlightedDateText="{Binding HighlightedDateText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                        Grid.Column="0"
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center"
                        Margin="0,0,0,0"
                        DateHighlightBrush="Gold">
  <i:Interaction.Triggers>
      <i:EventTrigger EventName="MouseLeftButtonDown">
          <cal:ActionMessage MethodName="CalendarSelectDateEvent">
             <cal:Parameter Value="{Binding ElementName=DemoCalendar, Path=SelectedDate}" />
            </cal:ActionMessage>
      </i:EventTrigger>
   </i:Interaction.Triggers>
</Controls:BasicCalendar>

以下是我想在点击日历中的某个日期后提出的事件:

public void CalendarSelectDateEvent(DateTime selectedDate)
{
  this.ActualCalendarEvents = this.CalendarEvents.Where(x => x.Date >= selectedDate).ToList();
  var a = this.ActualCalendarEvents;
  this.ActualCalendarEvents = null;
  NotifyOfPropertyChange(() => ActualCalendarEvents);
  this.ActualCalendarEvents = a;
  NotifyOfPropertyChange(() => ActualCalendarEvents);
}

当我尝试使用Click事件而不是MouseLeftButtonDown时,日历控件会自动处理它,而不会触发CalendarSelectDateEvent。有没有办法取消这两项活动?(对于日历控件,选择并突出显示日期,对于我,CalendarSelectDateEvent)

在组件外部单击后,日历鼠标左按钮向下被触发

我已经想好了。最后真的很容易。日历控件具有SelectedDate属性。我刚刚在代码隐藏中将SelectedDate绑定到我的属性SelectedDateChanged,当SelectedDate更改时,我引发事件CalendarSelectDateEvent。

            <Controls:BasicCalendar Grid.Row="0"
                                    x:Name="DemoCalendar" 
                                    DisplayDate="{Binding Path=DisplayDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                    HighlightedDateText="{Binding HighlightedDateText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                    Grid.Column="0"
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Center"
                                    Margin="0,0,0,0"
                                    SelectedDate="{Binding SelectedDateChanged}"
                                    DateHighlightBrush="Gold">
            </Controls:BasicCalendar>

像这样:

public DateTime SelectedDateChanged
{
  get
  {
    return selectedDateChanged;
  }
  set
  {
    this.selectedDateChanged = value;
    this.CalendarSelectDateEvent(this.selectedDateChanged);
  }
}