【问题标题】:Binding error with PropertyChanged - Object does not match target typePropertyChanged 绑定错误 - 对象与目标类型不匹配
【发布时间】:2020-08-03 16:13:42
【问题描述】:

我有一个带有双向绑定的文本框,但它给出了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 的数据上下文是:

this.DataContext = this;

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

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

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

使用 c# 4.6.1

【问题讨论】:

  • 创建绑定时,括号中的属性名称是为附加属性指定的。您的 eventDateFormat 属性是普通属性。要绑定到它,您必须首先创建一个 Config 类型的实例。究竟如何——我不能说。你提供的代码不够看懂。

标签: c# wpf binding inotifypropertychanged


【解决方案1】:

括号用于bind to attached properties

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

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

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

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

【讨论】:

  • 好的,很好。我删除了括号和“local:Config”,并将数据上下文设置为父网格上 Config 类的一个实例,它现在可以工作了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多