依赖属性更改了回调未触发

时间:2014-04-29 06:09:45

标签: wpf properties dependencies dependency-properties inotifypropertychanged

我有一个控件(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}"                            />

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

1 个答案:

答案 0 :(得分:0)

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

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