问题
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 Error: 40 : BindingExpression path error: 'Name' property not found on 'object' ''__ComObject' (HashCode=17252513)'. BindingExpression:Path=Name; DataItem='__ComObject' (HashCode=17252513); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
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>
Code behind
this.DataContext = viewModel;
this.viewModel = viewModel;
foreach (Configuration config in this.viewModel.Configurations)
{
Console.WriteLine(config.Name);
}
Can you help me out? Thank you.
回答1:
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.
回答2:
Because __ComObject
is mentioned in the error, I am presuming that the collection of objects you are binding to, are actually COM objects that implement the "Configuration" interface, rather than CLR ones.
(whether that COM object is actually implemented using C# (with ComVisible(true)) or C++ is immaterial, it's the fact you are using an "interface" - am presuming it is coming from an "COM interop library" i.e. after you did an add reference to a COM library?).
You can try this:
<TextBlock Text="{Binding (mynamespacewithinterface:Configuration.Name)}" />
Otherwise you can look at these links to understand the difficulty in the Binding system resolving bindings (COM interfaces and NET interfaces each have their own quirks/limitations), and some workarounds:
- https://social.msdn.microsoft.com/Forums/en-US/69389bb5-ba90-4a22-8a75-e1a21f1d3a16/comobject-in-datatemplate?forum=wpf
- http://badecho.com/2012/07/adding-interface-support-to-datatemplates/
- https://social.msdn.microsoft.com/Forums/vstudio/en-US/1e774a24-0deb-4acd-a719-32abd847041d/data-templates-and-interfaces
- WPF databinding to interface and not actual object - casting possible?
- https://social.msdn.microsoft.com/Forums/vstudio/en-US/1e774a24-0deb-4acd-a719-32abd847041d/data-templates-and-interfaces?forum=wpf
- http://blog.pmunin.com/2012/01/xaml-datatemplates-binding-to-interface.html
- http://leecampbell.blogspot.co.uk/2008/09/generic-binding-in-wpf-through-explicit.html
- http://www.dev102.com/2008/04/23/wpf-datatemplate-for-interfaces-not-supported/
来源:https://stackoverflow.com/questions/29163298/binding-to-a-com-object-cannot-resolve-a-property