依赖项属性已更改回调未启动



我有一个控件(DateRangeSelector),它按照以下代码注册依赖属性及其回调(TodayDateChanged):

DateRangeSelector.cs:

public static readonly DependencyProperty TodayDateProperty =
DependencyProperty.Register("TodayDate", typeof(bool),
                            typeof(DateRangeSelectorControl),
                            new PropertyMetadata(true, TodayDateChanged));
private static void TodayDateChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e) {
    ((DateRangeSelectorControl)d).TodayDateChanged(); 
}
public bool TodayDate { 
    get { return (bool)GetValue(TodayDateProperty); }
    set { SetValue(TodayDateProperty, value); } 
}

此控件在另一个XAML(ActivityListMenuControlView.XAML)中用作:

<DateRangeSelector:DateRangeSelectorControl x:Name="DateRangeSelector"
                                            Grid.Column="1"
                                            Margin="10 0 0 0"
                                            HorizontalAlignment="Left"
                                            VerticalAlignment="Center"
                                            AutomationProperties.AutomationId="AID_TaskListDateRangeSelector"
                                            DateRangeUpdatedCmd="{Binding Path=DateRangeSelectionUpdatedCommand}"
                                            FontSize="{StaticResource TaskListMenuFontSize}"
                                            RangeOptions="{Binding Path=DateRangeSelectionOptions,
                                                                   Mode=OneTime}"
                                            SelectedDateRange="{Binding Path=SelectedRange,
                                                                        Mode=TwoWay}"
                                            Visibility="{Binding Path=ShowFilterOptions,
                                                                 Converter={StaticResource boolToVisibility}}"
                                            TodayDate="{Binding TodayDate, ElementName=DateRangeSelector}" />

请注意,DateRangeSelector的依赖属性包装器"TodayDate"绑定到视图模型(ActivityListMenuControlViewModel)中另一个类似名称为"TodayDate"的属性并通过该属性公开。这是视图模型代码:

private bool m_UpdateTodayDate; 
public bool TodayDate
{
    get { return m_UpdateTodayDate; }
    set 
    {
        m_UpdateTodayDate = value;
        OnPropertyChanged("TodayDate");
    } 
}

现在,在另一个视图模型中,该属性每次都被指定相同的值:ActivityListContainerViewModel.cs:

private void RefreshModule(bool updateDateRangeSelectorCtrl)
{
    //"Today" filter date changed: Update DateRangeSelector
    if (updateDateRangeSelectorCtrl)
    {
        m_MenuControlViewModel.TodayDate = true;
    }
}

问题:DateRangeSelector中的属性更改回调"TodayDateChanged"从未被激发。我调试了代码,但控件从未命中此回调。

我在代码中做错了什么吗?

更新:根据"franssu"的评论,我更改了绑定如下:

<DateRangeSelector:DateRangeSelectorControl x:Name="DateRangeSelector" DataContext="MenuControlViewModel"                                     TodayDate="{Binding TodayDate,Mode=TwoWay}"                            />

仍然没有运气!没有回调命中。

<DateRangeSelector:DateRangeSelectorControl x:Name="DateRangeSelector"
[...]
TodayDate="{Binding TodayDate, ElementName=DateRangeSelector}" />

您将属性绑定到其本身,即控件的属性,而不是VM的属性。

始终在类的静态构造函数中注册DP。

相关内容

  • 没有找到相关文章

最新更新