WPF:如何在另一个依赖属性中使用依赖属性值?

发布于 2024-10-26 02:05:00 字数 4236 浏览 3 评论 0原文

好吧,这有点复杂。我创建了一个 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>

Code-behind:

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>

Code Behind:

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 中使用它。正如您在 MonthWeekControlWeekNumber 依赖属性的定义中看到的,它需要日期才能计算周数。

请帮忙。谢谢!

Ok, it's a tiny bit complex. I created a MonthViewControl user control:

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>

Code-behind:

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();
    }
}

Next, I created MonthWeekControl user control:

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>

Code behind:

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();
    }
}

The problem is that I don't know how to reach the Date dependency property from MonthViewControl in order to use it in MonthWeekControl. As you can see in the definition of the WeekNumber dependency property of the MonthWeekControl, it needs the date in order to calculate the week number.

Please help. Thanks!

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

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

发布评论

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

评论(2

余厌 2024-11-02 02:05:00

您拥有的这行代码是静态的。这意味着它只会执行一次 - 并且它不能引用任何其他非静态的东西。

public static readonly DependencyProperty WeekNumberProperty =
   DependencyProperty.Register("WeekNumber", typeof(int),         
     typeof(MonthWeekControl),         
     new UIPropertyMetadata(Utilities.GetWeekInYear(dateFromMonthViewControl)));

放弃此处的 UIPropertyMetadata - 这是用于为类的所有实例设置默认初始值。这在这种情况下是不合适的。

相反,让您的 MonthViewControl 迭代其每个 MonthWeekControl 并根据需要设置它们的 WeekNumber 属性。只要 MonthViewControl 的 Date 属性发生更改,就执行此操作。因此,现在的挑战是了解 Date 属性发生了什么变化...将用于注册此属性的 UIPropertyMetadata 更改为采用回调方法的属性。每当属性更改时都会调用此回调 - 然后您可以在其中设置 varios WeekNumber 值。有关详细信息,请参阅此处: http://msdn.microsoft.com/en-us/库/ms557330.aspx

This line of code that you have is static. that means it will only get executed once ever - and that it can't reference anything else that isn't static.

public static readonly DependencyProperty WeekNumberProperty =
   DependencyProperty.Register("WeekNumber", typeof(int),         
     typeof(MonthWeekControl),         
     new UIPropertyMetadata(Utilities.GetWeekInYear(dateFromMonthViewControl)));

Ditch the UIPropertyMetadata you have here - this is for setting a default initial value for all instances of the class. That isn't appropriate in this case.

Instead, have your MonthViewControl iterate over each of it's MonthWeekControl's and set the WeekNumber property on them as appropriate. Do that whenever the Date property of the MonthViewControl changes. So now the challenge is to know what the Date property changes... change the UIPropertyMetadata you use for registering this property to be one that takes a callback method. This callback will get invoked whenever the property changes - and then thats where you set the varios WeekNumber values. See here for details: http://msdn.microsoft.com/en-us/library/ms557330.aspx

2024-11-02 02:05:00

您可以在实例构造函数中设置绑定,而不是在 MonthWeekControl 中设置 WeekNumber 依赖属性的默认值:

public MonthWeekControl()
{
    InitializeComponent();
    SetBinding(WeekNumberProperty, new Binding("Date")
    {
        RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor,
            typeof (MonthViewControl)),
        Converter = /* an instance of Date-to-WeekNumber converter */
    });
}

Instead of setting the default value of WeekNumber dependency property in MonthWeekControl, you can set a binding in the instance constructor:

public MonthWeekControl()
{
    InitializeComponent();
    SetBinding(WeekNumberProperty, new Binding("Date")
    {
        RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor,
            typeof (MonthViewControl)),
        Converter = /* an instance of Date-to-WeekNumber converter */
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文