PropertyChanged 的​​绑定错误 - 对象与目标类型不匹配

Binding error with PropertyChanged - Object does not match target type

我有一个带有双向绑定的文本框,但它给出了 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"/>

这里是 class 和 属性:

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。我尝试绑定到它们所在的网格,但无法编译。

当我的配置 class 是静态的时,我能够进行双向绑定,而不是实现 INotifyPropertyChanged。但是,根据我的阅读,这个 class 不应该是静态的,因为它有时会更新。

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

使用 c# 4.6.1

括号用于bind to attached properties

To bind to an attached property, place parentheses around the attached property. For example, to bind to the attached property DockPanel.Dock, the syntax is Path=(DockPanel.Dock).

您需要从绑定中删除括号,以及命名空间前缀 local。假设 Config 是一个 属性 在你的主视图模型上持有你的 Config class 的实例,路径看起来像:

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

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