首页 > 解决方案 > PropertyChanged 绑定错误 - 对象与目标类型不匹配

问题描述

我有一个带有双向绑定的文本框,但它给出了TargetException错误。数据没有加载,TextBox当我输入它时,我得到这个错误:

System.Windows.Data Error: 8 : Cannot save value from target back to source. BindingExpression:Path=(local:Config.eventDateFormat); DataItem='String' (HashCode=-247125614); target element is 'TextBox' (Name='TXTB_Settings_Event_AddDateToName'); target property is 'Text' (type 'String') TargetException:'System.Reflection.TargetException: Object does not match target type.
   at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
   at MS.Internal.Data.PropertyPathWorker.SetValue(Object item, Object value)
   at System.Windows.Data.BindingExpression.UpdateSource(Object value)'

这是我的 XAML:

<TextBox x:Name="TXTB_Settings_Event_AddDateToName"
         Text="{Binding (local:Config.eventDateFormat), Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
         TextWrapping="Wrap"  Margin="110,200,1012,492"/>

这是类和属性:

public class Config : INotifyPropertyChanged
{
   private string _eventDateFormat = "MMM-dd-yy-";
   public string eventDateFormat
   {
      get { return _eventDateFormat; }
      set
      {
         if (value != _eventDateFormat)
         {
            _eventDateFormat = value;
            OnPropertyChanged(nameof(eventDateFormat));
         }
      }
   }

   public event PropertyChangedEventHandler PropertyChanged;
   protected void OnPropertyChanged(string name)
   {
      PropertyChangedEventHandler handler = PropertyChanged;
      if (handler != null)
      {
         handler(this, new PropertyChangedEventArgs(name));
      }
   }
}

我也试过这个:

private string _eventDateFormat = "MMM-dd-yy-";
public string eventDateFormat
{
   get { return _eventDateFormat; }
   set
   {
      if (value != _eventDateFormat)
      {
         _eventDateFormat = value;
         Changed();
      }
   }
}

public event PropertyChangedEventHandler PropertyChanged;
public void Changed([CallerMemberName] string property = "") =>
   PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));

MainWindow 的 DataContext 是:

this.DataContext = this;

我有一堆绑定到其他 UI 元素的可观察集合,所以我不能将 DataContext 更改为只是 Config。我尝试绑定到它们所在的网格,但无法编译。

当我的 Config 类是静态的时,我能够进行两种方式的绑定,而不是实现 INotifyPropertyChanged。但是,根据我的阅读,这个类不应该是静态的,因为它有时会更新。

不确定是否相关,我正在使用 MahApps Metro 2.0

使用 c# 4.6.1

标签: c#wpfbindinginotifypropertychanged

解决方案


括号用于绑定附加属性

要绑定到附加属性,请在附加属性周围放置括号。例如,要绑定到附加属性 DockPanel.Dock,语法为 Path=(DockPanel.Dock)。

您需要从绑定中删除括号以及命名空间前缀local。假设这Config是一个在主视图模型上保存类实例的属性Config,路径如下所示:

Text="{Binding Config.eventDateFormat, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

如果它不是您的主视图模型上的属性,如果您无法在任何地方明确将其设置为数据上下文,则将其设为一个。如果您将单独的实例设置为数据上下文,您只需绑定到eventDateFormat.


推荐阅读