【发布时间】:2011-05-21 07:48:24
【问题描述】:
难过:我正处于创建需要执行一些 UI 操作的月份计划器控件的初始阶段 I 当提供来自 XAML 中绑定的 ViewModel 的日期时的控件。当我运行代码时,ViewModel 确实返回了值,但 PropertyChangeCallBack 没有触发。我在public class ExtendedDataGrid : DataGrid 控件中做了完全相同的事情,并且工作正常,但几乎就像您必须对 UserControls 做一些不同的事情。这是我的代码的基础:
public partial class DiaryMonthViewUserControl : UserControl
{
private DateTime _topDate;
public DiaryMonthViewUserControl()
{
InitializeComponent();
}
// Property to get the date from the ViewModel for processing. This will fire and event to set the month we want to display
public static readonly DependencyProperty TheDateProperty = DependencyProperty.Register("TheDate", typeof(DateTime),
typeof(DiaryMonthViewUserControl),
new FrameworkPropertyMetadata(DateTime.Today, OnDatePropertyChanged, null));
public void SetTheCurrentDate(DateTime CurrentDate)
{
_topDate = CurrentDate;
// Like Outlook, whatever date we supply, I want the first top level displayed date for the month
if (_topDate.Day > 1)
_topDate = _topDate.AddDays(-(_topDate.Day-1)); // First day of the month
// Get to the first Monday before the 1st of the month if not on a Monday
while (_topDate.DayOfWeek != DayOfWeek.Monday)
_topDate = _topDate.AddDays(-1);
// I will set the UI here once I have solved this problem.
MakeChangesToTheUIPlease();
}
private static void OnDatePropertyChanged(DependencyObject Source, DependencyPropertyChangedEventArgs e)
{
var control = Source as DiaryMonthViewUserControl;
if (control != null)
control.SetTheCurrentDate((DateTime)e.NewValue);
}
[Bindable(true)]
public DateTime TheDate
{
get { return (DateTime)GetValue(TheDateProperty); }
set { SetValue(TheDateProperty, value); }
}
}
我也尝试使用new PropertyChangedCallback(OnDatePropertyChanged) 作为参数,但仍然不起作用。
我的绑定如下:
<my:DiaryMonthViewUserControl HorizontalAlignment="Left" Margin="12,12,0,0" x:Name="diaryMonthViewUserControl1"
VerticalAlignment="Top" Height="325" Width="476" TheDate="{Binding Path=CurrentDate}" />
当我运行代码时,我的 ViewModel 会在 CurrentDate 的 getter 上中断,如果我删除 CurrentDate 绑定,那么它不会。问题是回调没有触发,而且我这辈子都无法理解为什么。
任何帮助都将不胜感激,特别是可能涵盖此问题的文章的链接。
【问题讨论】:
-
你提到你可以在 getter 上中断,但回调不是会被触发 setter 的代码触发吗?
-
好的,我刚刚发现了一些不是答案但更接近的东西。如果我使用完全相同的代码,但将类型更改为 int,那么它是有效的,但 DateTime 类型不是。将 DateTime 类型作为依赖属性是否存在已知问题?
-
Wpf 新手,但 WiForms 编程的老手,我觉得我可以猜到它是如何工作的。我的 XAML 中的绑定到依赖属性调用视图模型上绑定属性的 getter,然后为依赖属性设置 GetValue() 和 SetValue() 中的值,然后它将触发更改的回调事件。 Binding 确实从视图模型中提取值,但根本不做任何其他事情。我想我一定是遗漏了一些本应公开的东西或将某些东西设为私有,因为我之前已经创建了几个依赖属性
-
然而,真正奇怪的是,当我将属性类型从 DateTime 更改为 string 或 int 然后更改所有其他相应的属性并调用以匹配该类型时,它可以工作。就像 DateTime 不适用于依赖属性,但我知道它们可以。在用户控件上创建依赖属性时我需要考虑什么?
标签: wpf xaml dependency-properties