绑定WPF控件以动态引入模型对象上的接口 [英] Binding WPF controls to dynamically introduced interfaces on model objects

查看:179
本文介绍了绑定WPF控件以动态引入模型对象上的接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PostSharp的[CompositionAspect]功能,动态地将接口​​注入到我的WPF应用程序中的模型对象中。但是,似乎WPF无法绑定(显示)属性,除非在模型对象上明确实现相关的界面?

I am using PostSharp's [CompositionAspect] capability to dynamically inject an interface to a model object in my WPF application. However, it seems WPF cannot bind (display) properties corre ctly unless the relevant interface is explicitly implemented on the model object?

使用自定义的CompositionAspect(ComposeObjectAspect)我可以通过运行时将ModelObject直接引导到引入的界面来成功公开内​​部的IMyInterface对象的属性;

Using a custom CompositionAspect (ComposeObjectAspect) I am able to successfully expose the internal IMyInterface object's properties directly by runtime casting the ModelObject to the introduced interface;

// some interface definition, for sample completeness
public interface IMyInterface 
{
    public string MyProperty { get; set; }
}


// model object, which does NOT implement IMyInterface directly, though the interface is dynamically introduced by PostSharp
[ComposeObjectAspect(typeof(IMyInterface))]
public class ModelObject 
{
    private IMyInterface actualDataObject;
}

. . .

public class ViewModel
{
    public ObservableCollection<IMyInterface> MyData { get; }

    public void LoadData()
    {
        // concrete IMyInterface dtos are returned here, and put into a VM collection
        IList<IMyInterface> myData = 
            (from IMyInterface o in SomeDataSource.LoadLocalDescriptors() 
             select new ModelObject(o) as IMyInterface).ToList();
        this.MyData = new ObservableCollection<IMyInterface>(myData);
    }
}

这一切都很好,如果我检查任何对象在myData列表中,或者将其转换为IMyInterface类型(可以从上面的类ModelObject 定义看到的定义不是直接实现的),我可以从 actualDataObject 直接。这一切都很好。

This all works fine, and if I inspect any object in the myData list, or cast it to the type IMyInterface (which as you can see form the class ModelObject definition above is not implemented directly), I can see the inner properties from actualDataObject directly. This all works great.

现在,当我尝试将ViewModel.Data集合绑定到WPF数据网格时,我会收到一系列绑定错误,如;

Now when I try to bind the ViewModel.Data collection to a WPF datagrid, I get a series of binding errors like;


System.Windows.Data错误:40:BindingExpression路径错误:'SomeProperty'属性未找到'object'''ModelObject'(HashCode = 56810243)' 。 BindingExpression:Path = SomeProperty; DataItem ='ModelObject'(HashCode = 56810243); target元素为'TextBlock'(Name ='');目标属性是'Text'(类型'String')

System.Windows.Data Error: 40 : BindingExpression path error: 'SomeProperty' property not found on 'object' ''ModelObject' (HashCode=56810243)'. BindingExpression:Path=SomeProperty; DataItem='ModelObject' (HashCode=56810243); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

为了参考,View XAML的相关部分看起来像; p>

For reference, the relevant section of the View XAML looks like;

<DataGrid ItemsSource="{Binding MyData}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Width="Auto" Header="Some Property" Binding="{Binding SomeProperty, Mode=OneWay}" />
    </DataGrid.Columns>
</DataGrid>

尽管所有数据行都可见(和为空),但是它们是空白的网格列。

It is the grid columns which are appearing blank, though all data rows are visible (and empty).

如果我检查'ModelObject',那么属性IMyInterface.SomeProperty就在那里。

If I inspect 'ModelObject', the property IMyInterface.SomeProperty is there.

如果我直接将IMyInterface应用于ModelObject类(并将所有成员作为简单的继承器插入组合的actualDataObject实例),那么绑定就很好,SomeProperty显示没有错误。

If I directly apply IMyInterface to the ModelObject class (and implenment all members as simple relays to the composed actualDataObject instance) then the binding is fine, and SomeProperty displays without error.

我错过了PostSharp实现IMyInterface和我自己的显式版本之间的区别?

What am I missing that is different between the PostSharp implementation of IMyInterface, and my own explicit version?

推荐答案

我在这里找到了解决这个问题的方法: WPF数据绑定到接口而不是实际的对象 - 可能的投入?

I managed to find a solution to this problem here: WPF databinding to interface and not actual object - casting possible?

如果定义了绑定将会起作用明确地反对接口类型,而不是隐式地,可能是因为ModelObject没有SomeProperty,但它确实有IMyInterface.SomeProperty?

Turns out the binding will work if it is defined explicitly against the interface type, instead of implicitly, presumably because ModelObject does not have a SomeProperty, but it does have IMyInterface.SomeProperty ?

所以,这个工作;

<DataGridTextColumn Width="Auto" Header="Some Property" 
       Binding="{Binding Path=(mynamespace:IMyInterface.SomeProperty), Mode=OneWay}" />

尽管如此,引用的问题确实会导致设计师警告。

Though, it does cause designer warnings, as noted in the referenced question.

这篇关于绑定WPF控件以动态引入模型对象上的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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