【发布时间】:2015-06-27 05:50:21
【问题描述】:
我有一个 wpf 应用程序,我想在其中绑定可绑定属性
<DatePicker HorizontalAlignment="Left" VerticalAlignment="Top" SelectedDate="{Binding DateP, Mode=TwoWay, UpdateSourceTrigger=LostFocus}">
<DatePicker.BlackoutDates>
<CalendarDateRange view:Facturation.End="{Binding DateE1, Mode=TwoWay}"/>
<CalendarDateRange view:Facturation.Start="{Binding DateS1, Mode=TwoWay}" view:Facturation.End="{Binding DateE2, Mode=TwoWay}"/>
</DatePicker.BlackoutDates>
</DatePicker>
在后面的代码中我添加了这个 sn-p :
public partial class Facturation : UserControl
{
public Facturation()
{
InitializeComponent();
}
public static readonly DependencyProperty StartProperty = DependencyProperty.Register("Start", typeof(DateTime), typeof(Facturation));
public static readonly DependencyProperty EndProperty = DependencyProperty.Register("End", typeof(DateTime), typeof(Facturation));
public DateTime End
{
get { return (DateTime)GetValue(EndProperty); }
set { SetValue(EndProperty, value); }
}
public DateTime Start
{
get { return (DateTime)GetValue(StartProperty); }
set { SetValue(StartProperty, value); }
}
}
当我启动应用程序时,我遇到了这个异常:
无法在“CalendarDateRange”类型的属性“End”上设置“Binding”。 “绑定”只能在 DependencyObject 的 DependencyProperty 上设置。
编辑
即使我创建了一个自定义日期选择器:
class SpecialDatePicker: DatePicker
{
public SpecialDatePicker()
{
}
public static readonly DependencyProperty StartProperty = DependencyProperty.Register("Start", typeof(DateTime), typeof(SpecialDatePicker));
public static readonly DependencyProperty EndProperty = DependencyProperty.Register("End", typeof(DateTime), typeof(SpecialDatePicker));
public static readonly DependencyProperty CalendarDateRangeProperty = DependencyProperty.Register("CalendarDateRange ", typeof(CalendarDateRange), typeof(SpecialDatePicker));
public DateTime End
{
get { return (DateTime)GetValue(EndProperty); }
set { SetValue(EndProperty, value); }
}
public DateTime Start
{
get { return (DateTime)GetValue(StartProperty); }
set { SetValue(StartProperty, value); }
}
public CalendarDateRange CalendarDateRange
{
get { return (CalendarDateRange)GetValue(CalendarDateRangeProperty); }
set { SetValue(CalendarDateRangeProperty, value); }
}
}
然后我像这样更改 xaml 文件:
<skin:SpecialDatePicker HorizontalAlignment="Left" VerticalAlignment="Top" SelectedDate="{Binding DateP, Mode=TwoWay, UpdateSourceTrigger=LostFocus}">
<skin:SpecialDatePicker.BlackoutDates>
<CalendarDateRange End="{Binding DateE1, Mode=TwoWay}"/>
<CalendarDateRange Start="{Binding DateS1, Mode=TwoWay}" End="{Binding DateE2, Mode=TwoWay}"/>
</skin:SpecialDatePicker.BlackoutDates>
我得到了同样的结果。
EDIT2
正如@Rachel 所说,我刚刚添加了这个转换器
class DatesToBlackoutDateCollection : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
CalendarBlackoutDatesCollection coll = new CalendarBlackoutDatesCollection(null);
DateTime datDebut =(DateTime) values.Min() ;
DateTime datFin =(DateTime) values.Max() ;
coll.Add(new CalendarDateRange(datDebut,datFin));
return coll;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
return (object[])value;
}
}
在 Xaml 文件中:
<DatePicker.BlackoutDates>
<MultiBinding Converter="{StaticResource DatesToBlackoutDateCollection}">
<Binding Path="DateS1" />
<Binding Path="DateE2"/>
</MultiBinding>
</DatePicker.BlackoutDates>
但没有变化,出现了类似的异常。
我需要知道:
- 这个错误的原因是什么?
- 我该如何解决?
【问题讨论】:
-
原因是
CalendarDateRange不是继承自DependencyObject -
@dkozl 那么我该如何修复我的代码
-
如果你在依赖属性定义上使用
.RegisterAttached而不是.Register,会有什么不同吗?如果您使用.RegisterAttached,我认为您只能将属性附加到另一个对象类型 -
@Rachel 不,我得到了同样的例外
标签: c# wpf xaml binding dependency-properties