2011-03-03 41 views
0

我發展,我的生活都是這樣揭露自定義DateTime屬性的用戶控件

<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對在一個字典,並讓他們也自動生成如果可能。任何幫助是極大的讚賞:)

感謝, 尼克拉斯

回答

1

你應該暴露在控制一個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> 

然後DataBind它從代碼behi nd:

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

這是完美的。非常感謝:D一個跟隨的問題。我想將這個改變傳播到我的DayView上的DataContext。有沒有辦法通過使用OneWay綁定來約束這個?謝謝 – Bakery 2011-03-03 13:32:43

+0

我不明白你想要做什麼,你能深入解釋一下嗎? (另外,將問題標記爲回答:) – 2011-03-03 17:29:18

+0

我有一個包含DayViews列表的日曆視圖(感謝您:D)。 CalendarView使用類似的綁定來設置DayView的日期。現在我希望能夠從CalendarView代碼背後更改這一天,並且在DayView.DataContext上觸發屬性更新。就像如果我有一個屬性,CurrentDay,在DayViewModel我想更改Day DependencyProperty時更新。我想我可以提出一個新的問題:)謝謝。我會將其標記爲答案。我只是想等一下,看看我是否有更多的答案:) – Bakery 2011-03-04 11:52:20

相關問題