绑定到 COM 对象 - 无法解析属性 [英] Binding to a COM Object - Cannot resolve a Property

查看:25
本文介绍了绑定到 COM 对象 - 无法解析属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在将对象绑定到 WPF 列表框时遇到了一些麻烦.元素数量已正确填充,但无法访问 Name 属性.我收到以下错误消息:

I am currently running into some trouble with binding objects to a WPF ListBox. The number of Elements is populated correctly but the Name property cannot be accessed. I get the following error message:

System.Windows.Data 错误:40:BindingExpression 路径错误:在object"__ComObject"(HashCode=17252513)上找不到Name"属性.绑定表达式:路径=名称;数据项='__ComObject'(哈希码=17252513);目标元素是 'TextBlock' (Name='');目标属性是文本"(类型字符串")

但是,我可以在运行时将此属性正确打印到控制台.

However, I am able to print this property correctly to the console at runtime.

看起来对象没有正确投射

It looks like the object is not casted correctly

XAML:

<ListBox Name="lbSelectConfiguration" Margin="10" ItemsSource="{Binding Configurations}">
  <ListBox.ItemTemplate>
      <StackPanel Margin="5" Orientation="Horizontal">
        <Image Source="/Resources/Configuration.png" Margin="0,0,8,0" />
        <TextBlock Text="{Binding Name}" />
      </StackPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

背后的代码

  this.DataContext = viewModel;
  this.viewModel = viewModel;
  foreach (Configuration config in this.viewModel.Configurations)
  {
      Console.WriteLine(config.Name);
  }

你能帮我吗?谢谢.

推荐答案

据我所知,ComObjects 在 .NET 中是 dynamic,所以编译器永远不会在编译时识别对象属性的名称.如果这对您来说真的很重要,那么只需创建一个实现 INotifyPropertyChanged 接口的 .NET 包装器对象并将该对象的所有属性复制到您的 WPF 对象.显然,编译器将能够读取属性的名称.

As far as I can remember, ComObjects are dynamic in .NET, so the compiler will never recognise the names of the object's properties at compile time. If that's really important for you, then just create a .NET wrapper object that implements the INotifyPropertyChanged interface and copy all of the object's properties to your WPF object. The compiler will then obviously be able to read the names of the properties.

这篇关于绑定到 COM 对象 - 无法解析属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆