[英]Expose custom DateTime property to user control
我正在开发一个日历,使我的日子像这样
<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}"/>
最好我想将所有custom:DayDisplay对存储在Dictionary中,并在可能的情况下自动生成它们。 任何帮助是极大的赞赏 :)
谢谢,尼克拉斯
您应该在控件上公开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));
如果要自动生成,则应该做一个ItemsControl,其中包含您的dayControl,如下所示:
<ItemsControl x:Name="DaysItemsControl">
<ItemsControl.ItemTemplate>
<DataTemplate>
<custom:DayDisplay Day="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
然后从后面的代码中将其绑定:
DateTime[] week = new DateTime[] { new DateTime(2000,1,1), new DateTime(200,1,2) };
DayItemsControl.ItemsSource = week;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.