【发布时间】:2011-07-22 04:19:06
【问题描述】:
好的,这有点复杂。我创建了一个MonthViewControl 用户控件:
Xaml:
<UserControl x:Class="MonthView.Controls.MonthViewControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="clr-namespace:MonthView.Controls"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<!-- The following line is important! -->
<TextBlock Text="{Binding Path=Date, Converter={...}}" />
<ItemsControl>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="6" Columns="1" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<controls:MonthWeekControl />
<controls:MonthWeekControl />
<controls:MonthWeekControl />
<controls:MonthWeekControl />
<controls:MonthWeekControl />
<controls:MonthWeekControl />
</ItemsControl>
</UserControl>
代码隐藏:
public partial class MonthViewControl : UserControl
{
public static readonly DependencyProperty DateProperty =
DependencyProperty.Register("Date", typeof(DateTime),
typeof(MonthViewControl),
new UIPropertyMetadata(DateTime.Today));
public DateTime Date
{
get { return (DateTime)GetValue(DateProperty); }
set { SetValue(DateProperty, value); }
}
public MonthViewControl()
{
InitializeComponent();
}
}
接下来,我创建了MonthWeekControl用户控件:
Xaml:
<UserControl x:Class="MonthView.Controls.MonthWeekControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="clr-namespace:MonthView.Controls"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Border Grid.Column="0">
<!-- The following line is important! -->
<TextBlock Text="{Binding Path=WeekNumber}" />
</Border>
<ItemsControl Grid.Column="1">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="1" Columns="7" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<controls:MonthDayControl />
<controls:MonthDayControl />
<controls:MonthDayControl />
<controls:MonthDayControl />
<controls:MonthDayControl />
<controls:MonthDayControl />
<controls:MonthDayControl />
</ItemsControl>
</Grid>
</UserControl>
后面的代码:
public partial class MonthWeekControl : UserControl
{
public static readonly DependencyProperty WeekNumberProperty =
DependencyProperty.Register("WeekNumber", typeof(int),
typeof(MonthWeekControl),
new UIPropertyMetadata(Utilities.GetWeekInYear(dateFromMonthViewControl)));
// Utilities.GetWeekInYear(DateTime date) gets the week number
// based on the provided date
public int WeekNumber
{
get { return (int)GetValue(WeekNumberProperty); }
set { SetValue(WeekNumberProperty, value); }
}
public MonthWeekControl()
{
InitializeComponent();
}
}
问题是我不知道如何从MonthViewControl 访问Date 依赖属性以便在MonthWeekControl 中使用它。正如您在MonthWeekControl 的WeekNumber 依赖属性的定义中看到的那样,它需要日期才能计算周数。
请帮忙。谢谢!
【问题讨论】:
标签: wpf wpf-controls binding dependency-properties