wpf 应用程序中的依赖性 属性 问题

Dependency Property issue within wpf application

我有一个 wpf 应用程序,我想在其中绑定 inbindable 属性

      <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>

在后面的代码中我添加了这个片段:

 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); }

    }
}

当我启动应用程序时,我遇到了这个异常:

Unable to set 'Binding' on property 'End' of type 'CalendarDateRange'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

编辑

甚至我创建了一个自定义日期选择器:

 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>

但是没有变化,出现了类似的异常。

我需要知道:

  1. 这个错误的原因是什么?
  2. 我该如何解决?
  1. 错误原因是什么?

    • 您只能绑定到依赖项 属性。 CalendarDateRange 不是依赖项 属性。
  2. 我该如何解决?

    • 扩展现有的 DatePicker 控件以创建具有 CalendarDateRangeDependency 属性的自定义 DatePicker。或者既然你提到了代码背后,猜测你没有使用 MVVM 模式,只需在代码背后分配黑色日期。

当然,推荐的模式是创建一个自定义 DatePicker 控件,其中包含不适用日期的依赖属性。

编辑

您还可以创建 attached 依赖关系 属性 并将其用于您的日历限制日期。 如果需要,我将提供有关如何使用自定义 DatePicker 控件(更多编码)执行此操作的代码,否则,您可以按照其他答案中提供的附加 属性 方式(更简单)。

对象 CalendarDateRange does not inherit from DependencyObject,因此它不参与 WPF 的绑定系统。这意味着您不能在其上绑定任何属性。

我建议使用 IMultiValueConverter 绑定 BlackoutDates 属性 以将您的日期转换为 CalendarBlackoutDatesCollection

<DatePicker.BlackoutDates>
     <MultiBinding Converter="{StaticResource DatesToBlackoutDateCollection}"> 
           <Binding Path="DateE1" />
           <Binding Path="DateS1"/>
     </MultiBinding>
 </DatePicker.BlackoutDates>

或者,您可以尝试在 DatePicker 对象上创建附加属性,并使用代码隐藏将数据传输到 DatePicker.BlackoutDates 属性.