简体   繁体   English

如何在此日历视图中将列表添加到特定日期?

[英]How to add a list in this calendarview to a specific date?

I am working in a calender application in winui and need to add a list on a specific date我在 winui 中的日历应用程序中工作,需要在特定日期添加列表

I have tried adding a text block and next I would like to change it to a list我尝试添加一个文本块,接下来我想将其更改为列表

You can create a user control like this one:您可以像这样创建一个用户控件:

CustomCalendarView.xaml CustomCalendarView.xaml

<UserControl
    x:Class="CalendarViewTests.CustomCalendarView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="using:CalendarViewTests"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid>
        <CalendarView
            x:Name="CalendarViewControl"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch"
            CalendarItemBorderBrush="DimGray"
            CalendarItemBorderThickness="1"
            CalendarItemCornerRadius="0"
            DayItemFontSize="10"
            DayItemFontWeight="ExtraLight"
            HorizontalDayItemAlignment="Center"
            VerticalDayItemAlignment="Top">
            <CalendarView.CalendarViewDayItemStyle>
                <Style TargetType="CalendarViewDayItem">
                    <Setter Property="FontSize" Value="10" />
                    <Setter Property="FontWeight" Value="ExtraLight" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Grid
                                    Margin="5"
                                    VerticalAlignment="Center">
                                    <ListView ItemsSource="{Binding}" />
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </CalendarView.CalendarViewDayItemStyle>
        </CalendarView>
    </Grid>
</UserControl>

CustomCalenderView.xaml.cs CustomCalenderView.xaml.cs

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.Generic;
using System.Linq;

namespace CalendarViewTests;

public record CustomCalendarViewDayItem
{
    public CustomCalendarViewDayItem(DateTime dateTime, string text)
    {
        DateTime = dateTime;
        Text = text;
    }

    public DateTime DateTime { get; }

    public string Text { get; }
}

public sealed partial class CustomCalendarView : UserControl
{
    public static readonly DependencyProperty DayItemsProperty = DependencyProperty.Register(
        nameof(DayItems),
        typeof(IEnumerable<CustomCalendarViewDayItem>),
        typeof(CustomCalendarView),
        new PropertyMetadata(default));

    public CustomCalendarView()
    {
        InitializeComponent();
        this.CalendarViewControl.CalendarViewDayItemChanging += CalendarViewControl_CalendarViewDayItemChanging;
    }

    public IEnumerable<CustomCalendarViewDayItem> DayItems
    {
        get => (IEnumerable<CustomCalendarViewDayItem>)GetValue(DayItemsProperty);
        set => SetValue(DayItemsProperty, value);
    }

    private void CalendarViewControl_CalendarViewDayItemChanging(CalendarView sender, CalendarViewDayItemChangingEventArgs args)
    {
        if (DayItems.Where(x => DateOnly.FromDateTime(x.DateTime) == DateOnly.FromDateTime(args.Item.Date.Date))
            .Select(x => x.Text) is IEnumerable<string> dayItems)
        {
            args.Item.DataContext = dayItems;
        }
    }
}

and use it like this:并像这样使用它:

MainPage.xaml主页.xaml

<Page
    x:Class="CalendarViewTests.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="using:CalendarViewTests"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
    mc:Ignorable="d">

    <Grid>
        <local:CustomCalendarView DayItems="{x:Bind DayItems, Mode=OneWay}" />
    </Grid>
</Page>

MainPage.xaml.cs MainPage.xaml.cs

using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.ObjectModel;

namespace CalendarViewTests;

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        DayItems.Add(new CustomCalendarViewDayItem(DateTime.Now.AddDays(-5), "5 days ago"));
        DayItems.Add(new CustomCalendarViewDayItem(DateTime.Now, "Todo 1"));
        DayItems.Add(new CustomCalendarViewDayItem(DateTime.Now, "Todo 2"));
        DayItems.Add(new CustomCalendarViewDayItem(DateTime.Now, "Todo 3"));
        DayItems.Add(new CustomCalendarViewDayItem(DateTime.Now, "Todo 4"));
        DayItems.Add(new CustomCalendarViewDayItem(DateTime.Now, "Todo 5"));
        DayItems.Add(new CustomCalendarViewDayItem(DateTime.Now.AddDays(1), "Tommorrow"));
    }

    public ObservableCollection<CustomCalendarViewDayItem> DayItems { get; } = new();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们如何在winui中获取父对象(calendarview)的子对象(calendardayitem)? - How can we get the children(calendardayitem) of parent object(calendarview) in winui? Powershell脚本:列出具有特定更改日期的文件(可能的话,数量) - Powershell script: List files with specific change date (Amount if possible) 如何将程序添加到“执行”列表 - How to add a program to the 'execute' list 如何将用户添加到Active Directory中的特定域 - How to add user to a specific domain in Active Directory 如何从 MS Access(数据库)获取数据到具有特定日期的 dataGridView? - How to fetch data from MS Access (Database) to dataGridView with specific date? 如何使用Powershell 2.0复制早于特定日期的文件夹 - How to copy folders older than a specific date with Powershell 2.0 如何在月历C#中手动设置特定的开始日期 - How to set specific starting date in Month Calendar C# manually 如何在特定日期最后更改的文件中查找字符串? - How to find a String in a file which was lastly changed on a specific date? 如何在 windows 服务器 2012 的日期范围之间查找特定文件, - How to find a specific file between a date range in windows server 2012, 如何在MFC的CTreeCtrl列表中添加图像 - How to add image in CTreeCtrl list in MFC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM