[英]WPF calendar deselects selected dates on LostFocus
我在WPF项目中添加了一个日历控件,如下所示:
<Calendar x:Name="DatesCalendar" SelectionMode="SingleRange" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center" Margin="0,10" VerticalAlignment="Center" />
这个想法是当用户选择一个日期范围并单击搜索按钮时,我从数据库中获取该范围内的条目。 此刻,每当用户单击搜索按钮时,“日历”日期就会被取消选择。 当日历失去焦点时,我希望最后选择的日期保持选中状态。
我在这里了解到有关此问题的可能答案。 为了完整起见,我复制那里提供的信息:
Calendar.SelectedDates
是一个只读属性 。 因此,不能在XAML
中将Binding
设置为它。
但是,您可以通过以下方式进行操作:
1)将System.Windows.Interactivity.dll
添加到您的项目引用中
2)将Microsoft.Expression.Interactions.dll
添加到您的项目引用中
3)在您的XAML中添加以下namespace
前缀:
`xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"`
`xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"`
4) XAML :
<sdk:Calendar Name="Cal" SelectionMode="MultipleRange" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectedDatesChanged">
<i:InvokeCommandAction Command="{Binding SelectionCommand}"
CommandParameter="{Binding SelectedDates, ElementName=Cal}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</sdk:Calendar>`
5) ViewModel :
public class ViewModel
{
public ViewModel()
{
this.SelectionCommand = new DelegateCommand<object>(this.SelectionChanges );
}
public ICommand SelectionCommand { get; private set; }
private void SelectionChanges(object sender)
{
SelectedDatesCollection dates = sender as SelectedDatesCollection;
}
}
我自己还没有测试过。 有帮助吗? 此答案顶部的链接中有更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.