开发者

Expose custom DateTime property to user control

开发者 https://www.devze.com 2023-02-14 08:28 出处:网络
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 Resource

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 prefer开发者_如何学JAVAably 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


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消