Xaml 如何在UWP中只读CalendarView控件

Xaml 如何在UWP中只读CalendarView控件,xaml,calendar,uwp,uwp-xaml,Xaml,Calendar,Uwp,Uwp Xaml,在我的UWP应用程序中,我添加了一个日历视图,并通过编程选择了几个日期。现在我想让它对最终用户是只读的。但是该控件不提供此类功能,并且使IsEnabled=False不是一个选项。 请告诉我一种使控件只读的方法。 谢谢 注意:我的键盘有这个问题。当用户按enter键或空格键时,日历将更改其视图。我想让它成为静态的。你也可以试试 YourCalendarView.IsHitTestVisible = false; 或 将IsEnabled设置为False是正确的选择。我猜您不想使用它的原因是因为

在我的UWP应用程序中,我添加了一个日历视图,并通过编程选择了几个日期。现在我想让它对最终用户是只读的。但是该控件不提供此类功能,并且使IsEnabled=False不是一个选项。 请告诉我一种使控件只读的方法。 谢谢

注意:我的键盘有这个问题。当用户按enter键或空格键时,日历将更改其视图。我想让它成为静态的。

你也可以试试

YourCalendarView.IsHitTestVisible = false;


IsEnabled
设置为
False
是正确的选择。我猜您不想使用它的原因是因为这样做会使整个控件变暗。因此,真正的问题归结为-如何禁用控件,同时保持其外观不变

通常,禁用(即
IsEnabled=“False”
)控件的外观在其样式内的
disabled
状态中定义。不过,在您的情况下,事情会变得有点复杂,因为
CalendarView
是一个复杂的控件,因此需要更新多个样式

以下是如何做到这一点

首先,您需要找到
CalendarView
控件的名称,并将其放入
App.xaml
中。然后,搜索
,并注释掉此可视状态中的所有内容。应该有两个,因为第一个用于向上/向下按钮,第二个用于每周的所有日期文本

除此之外,还需要手动更新每个
CalendarViewDayItem
的颜色,因为用于显示日数的
TextBlock
不在其样式内

要在代码中执行此操作,请转到我的答案并复制
子类
扩展方法,然后只需在
加载的
事件中运行以下代码。注意:您可能希望给它一点延迟,以便正确应用
Disabled
状态

Loaded += async (s, e) =>
{
    await Task.Delay(100);
    var dayItems = MyCalendarView.Children().OfType<CalendarViewDayItem>();
    foreach (var dayItem in dayItems)
    {
        var textBlock = dayItem.Children().OfType<TextBlock>().Single();
        textBlock.Foreground = new SolidColorBrush(Colors.Black);
    }
};
load+=async(s,e)=>
{
等待任务。延迟(100);
var dayItems=MyCalendarView.Children(),of type();
foreach(var dayItem中的dayItem)
{
var textBlock=dayItem.Children().OfType().Single();
textBlock.Foreground=新的SolidColorBrush(Colors.Black);
}
};
完成所有这些操作后,您的
CalendarView
控件现在将在实际禁用时正常显示


希望这有帮助

对于任何正在寻找完整代码的人来说,这里就是。这一切都要感谢你

static int minOne=-1;
静态int plsOne=1;
公共静态异步任务ManipCalenderUserInterface(列出日历,列出日期ListByMonth)
{
尝试
{
corespatcher corespatcher=Window.Current.Dispatcher;
等待任务。运行(异步()=>
{
等待任务。延迟(1000);
//coreDispatcher是必需的,因为您不在主线程上,并且无法从工作线程管理UI
等待coreDispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
int-cnt=0;
bool cont=false;
bool-stop=false;
//突出显示颜色。我从我的资源文件中获取它
SolidColorBrush colorBrush=(SolidColorBrush)Application.Current.Resources[“FgBlue1”];
//这段代码是用来操作多个日历的
foreach(日历中的var项目)
{
var dayItems=item.Children(),of type();
var firstDayOfMonth=新的日期时间(dayItems.Skip(15).First().Date.Year,dayItems.Skip(15).First().Date.Month,plsOne);
var lastDayOfMonth=月的第一天添加月份(plsOne).AddDays(minOne);
foreach(var dayItem中的dayItem)
{
var textBlock=dayItem.Children().OfType().Single();
int dayNum=System.Convert.ToInt32(textBlock.Text);
如果(dayNum==1&&!stop)cont=true;

如果(cont&&dayNum>=1&&dayNum我知道这很晚了,但是假设您希望用户仍然能够选择和滚动月份等。这就是您需要做的所有事情:

<CalendarView SelectionMode="Multiple">
    <CalendarView.Resources>
        <Style TargetType="CalendarViewDayItem">
            <Setter Property="IsHitTestVisible"
                    Value="False" />
            <Setter Property="IsTabStop"
                    Value="False" />
        </Style>
    </CalendarView.Resources>
</CalendarView>

ishitestvisible
设置器将确保不能单击或点击
DayViewItem
,并且
IsTabStop
设置器将防止键盘聚焦于它


这允许您在禁用用户日期选择的同时保留完整的
日历视图
功能。

这仅适用于触摸。我在键盘上遇到了这个问题。当用户按enter键或空格键时,日历会更改其视图。我想将其设置为静态。您是否尝试重新运行代码?您需要确定是哪一个h day项超出范围,然后相应地更新颜色。如果没有API说明,请检查文本块的前景色。是否动态更新选择?如果我在xaml中将其设置为倍数,它仍然有效。您只需在短时间延迟后重新运行代码。可能需要将延迟增加到
200
,以确保这一点。我对它进行了测试,效果很好。非常感谢。我将延迟时间增加到1秒,现在效果非常好。您节省了我很多时间。:)干杯!将这段代码放入任务中,并在每次需要时重新运行。您可能只需创建任务并等待它,而无需使用任务。运行我想。:)@JustinXL我遇到了这种方法的一个问题。在Windows10Build10240上,它的工作方式就像一个迷人的bu
static int minOne = -1;
    static int plsOne = 1;
    public static async Task ManipCalenderUserInterface(List<CalendarView> calenders, List<List<DateTime>> datesListByMonth)
    {
        try
        {

                CoreDispatcher coreDispatcher = Window.Current.Dispatcher;
                await Task.Run(async () =>
                {
                    await Task.Delay(1000);
                    //coreDispatcher is required since you're not on the main thread and you can't manip UI from worker threads
                    await coreDispatcher.RunAsync(CoreDispatcherPriority.Normal,
                        () =>
                        {
                            int cnt = 0;
                            bool cont = false;
                            bool stop = false;
                            //Highlight color. I get it from my resource file
                            SolidColorBrush colorBrush = (SolidColorBrush)Application.Current.Resources["FgBlue1"];

                            //This code wrote to manipulate multiple calenders
                            foreach (var item in calenders)
                            {
                                var dayItems = item.Children().OfType<CalendarViewDayItem>();
                                var firstDayOfMonth = new DateTime(dayItems.Skip(15).First().Date.Year, dayItems.Skip(15).First().Date.Month, plsOne);
                                var lastDayOfMonth = firstDayOfMonth.AddMonths(plsOne).AddDays(minOne);

                                foreach (var dayItem in dayItems)
                                {
                                    var textBlock = dayItem.Children().OfType<TextBlock>().Single();

                                    int dayNum = System.Convert.ToInt32(textBlock.Text);
                                    if (dayNum == 1 && !stop) cont = true;

                                    if (cont && dayNum >= 1 && dayNum <= lastDayOfMonth.Day)
                                    {
                                        textBlock.Foreground = new SolidColorBrush(Colors.Black);
                                        //Bold the today date on this month's calender
                                        if (dayItem.Date == DateTime.Now.Date)
                                        {
                                            textBlock.FontWeight = FontWeights.Bold;
                                        }

                                        //datesListByMonth: contains all the dates need to be highlighted on th calender
                                        if (datesListByMonth[cnt] != null && datesListByMonth[cnt].Contains(dayItem.Date.Date))
                                            dayItem.Background = colorBrush;
                                    }
                                    if (cont && dayNum == lastDayOfMonth.Day)
                                    {
                                        cont = false;
                                        stop = true;
                                    }
                                }
                                cnt++;
                                cont = false;
                                stop = false;
                            }
                        });
                });

        }
        catch (Exception ex)
        {

        }
    }
<CalendarView SelectionMode="Multiple">
    <CalendarView.Resources>
        <Style TargetType="CalendarViewDayItem">
            <Setter Property="IsHitTestVisible"
                    Value="False" />
            <Setter Property="IsTabStop"
                    Value="False" />
        </Style>
    </CalendarView.Resources>
</CalendarView>