【问题标题】:Dependency Property issue within wpf applicationwpf应用程序中的依赖属性问题
【发布时间】: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>

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

我需要知道:

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

【问题讨论】:

  • 原因是CalendarDateRange不是继承自DependencyObject
  • @dkozl 那么我该如何修复我的代码
  • 如果你在依赖属性定义上使用.RegisterAttached而不是.Register,会有什么不同吗?如果您使用 .RegisterAttached,我认为您只能将属性附加到另一个对象类型
  • @Rachel 不,我得到了同样的例外

标签: c# wpf xaml binding dependency-properties


【解决方案1】:
  1. 错误的原因是什么?

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

    • 扩展现有的DatePicker 控件以创建具有CalendarDateRangeDependency 属性的自定义DatePicker。或者由于您提到后面的代码,猜测您没有使用 MVVM 模式,只需在后面的代码中分配停电日期。

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

编辑

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

【讨论】:

  • 谢谢,您能否详细说明您对当前 datepicker 扩展的想法,请查看我的编辑
【解决方案2】:

对象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 属性。

【讨论】:

  • 谢谢,但我得到了一个例外 Can not use a 'MultiBinding' in collection 'CalendarBlackoutDates Collection'. A 'MultiBinding' can only be set on a DependencyProperty of a DependencyObject. 。请看一下我的 EDIT2 部分
  • @LamloumiAfif 啊,听起来 BlackoutDates 属性不是 DependencyProperty。在这种情况下,我建议为DatePicker 控件创建一个附加属性,然后让代码将绑定值传输到BlackoutDates 集合。一个快速的谷歌搜索结果是this SO question,它有一些看起来不错的答案,可能对你有用:)
猜你喜欢
  • 2013-01-31
  • 2011-06-29
  • 1970-01-01
  • 2019-04-17
  • 2010-11-11
  • 2010-10-09
  • 1970-01-01
  • 2015-05-28
  • 1970-01-01
相关资源
最近更新 更多