【问题标题】:Dependency property changed callback not firing依赖属性更改回调未触发
【发布时间】:2014-06-14 22:16:15
【问题描述】:

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

日期范围选择器.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}"                            />

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

【问题讨论】:

    标签: wpf properties dependencies dependency-properties inotifypropertychanged


    【解决方案1】:
    <DateRangeSelector:DateRangeSelectorControl x:Name="DateRangeSelector"
    [...]
    TodayDate="{Binding TodayDate, ElementName=DateRangeSelector}" />
    

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

    【讨论】:

    • 你能提供一个代码 sn-p 来说明它应该是怎样的吗?
    • TodayDate与你绑定的其他属性有什么不同,比如DateRangeSelectionOptionsSelectedRangeShowFilterOptions
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2018-07-21
      • 2013-06-02
      • 2012-01-31
      • 2011-02-13
      • 1970-01-01
      • 1970-01-01
      • 2015-04-02
      相关资源
      最近更新 更多