向用户控件公开自定义 DateTime 属性

发布于 2024-10-20 02:50:45 字数 911 浏览 1 评论 0原文

我正在开发一个日历,其中我的日子是这样排列的

    <Label Grid.Row="1" Grid.Column="1" Content="{Binding Path=SundayLabel, FallbackValue=Sunday}" Style="{StaticResource ResourceKey=DayLabelStyle}" />
    <custom:DayDisplay Grid.Row="2" Grid.Column="1" x:Name="SundayDisplay" />

    <Label Grid.Row="1" Grid.Column="2" Content="{Binding Path=MondayLabel, FallbackValue=Monday}"  Style="{StaticResource ResourceKey=DayLabelStyle}" />
    <custom:DayDisplay Grid.Row="2" Grid.Column="2" x:Name="MondayDisplay" />

等等。 DayDisplay 是一个自定义 WPF 控件。 包含所有这些 DayDisplays 的控件需要某种方式来设置带有 DateTime 的各个日期,最好是从代码隐藏。

有没有某种方法可以在我的 DayDisplay 视图中公开自定义属性,以便我可以执行以下操作:

    <custom:DayDisplay Grid.Row="2" Grid.Column="2" x:Name="MondayDisplay" x:Day="{MondayDateTime}"/>

最好我想将所有自定义:DayDisplay 对存储在字典中,并在可能的情况下自动生成它们。非常感谢任何帮助:)

谢谢, 尼克拉斯

I am developing a calendar where my days are aligned like this

    <Label Grid.Row="1" Grid.Column="1" Content="{Binding Path=SundayLabel, FallbackValue=Sunday}" Style="{StaticResource ResourceKey=DayLabelStyle}" />
    <custom:DayDisplay Grid.Row="2" Grid.Column="1" x:Name="SundayDisplay" />

    <Label Grid.Row="1" Grid.Column="2" Content="{Binding Path=MondayLabel, FallbackValue=Monday}"  Style="{StaticResource ResourceKey=DayLabelStyle}" />
    <custom:DayDisplay Grid.Row="2" Grid.Column="2" x:Name="MondayDisplay" />

and so forth. DayDisplay is a custom WPF control.
The control containing all theese DayDisplays needs some way to set the individual days with a DateTime preferably from code behind.

Is there some way to expose a custom property in my DayDisplay view so i could do something like:

    <custom:DayDisplay Grid.Row="2" Grid.Column="2" x:Name="MondayDisplay" x:Day="{MondayDateTime}"/>

Preferably i would like to store all custom:DayDisplay pairs in a Dictionary and have them autogenerated also if it is possible. Any help is greatly appreciated :)

Thanks,
Niklas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_蜘蛛 2024-10-27 02:50:45

您应该在控件上公开一个 DateTime 依赖属性,如下所示:

    public DateTime Day
    {
        get { return (DateTime)GetValue(DayProperty); }
        set { SetValue(DayProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Day.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty DayProperty =
        DependencyProperty.Register("Day", typeof(DateTime), typeof(DayOfWeek), new UIPropertyMetadata(DateTime.MinValue));

如果您希望自动生成它,您应该做的是拥有一个包含您的 DayControl 的 ItemsControl,如下所示:

    <ItemsControl x:Name="DaysItemsControl">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <custom:DayDisplay Day="{Binding}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

然后从后面的代码对其进行 DataBind:

        DateTime[] week = new DateTime[] { new DateTime(2000,1,1), new DateTime(200,1,2) };
        DayItemsControl.ItemsSource = week;

you should expose a DateTime dependency property on the control, done like so:

    public DateTime Day
    {
        get { return (DateTime)GetValue(DayProperty); }
        set { SetValue(DayProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Day.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty DayProperty =
        DependencyProperty.Register("Day", typeof(DateTime), typeof(DayOfWeek), new UIPropertyMetadata(DateTime.MinValue));

if you want this to be auto generated, what you should do is have an ItemsControl that contains this dayControl of yours, like so:

    <ItemsControl x:Name="DaysItemsControl">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <custom:DayDisplay Day="{Binding}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

and then DataBind it from code behind:

        DateTime[] week = new DateTime[] { new DateTime(2000,1,1), new DateTime(200,1,2) };
        DayItemsControl.ItemsSource = week;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文