WPF databinding to interface and not actual object - casting possible?

前端 未结 6 1486
时光取名叫无心
时光取名叫无心 2020-11-27 05:27

Say I have an interface like this:

public interface ISomeInterface
{
...
}

I also have a couple of classes implementing this interface;

6条回答
  •  没有蜡笔的小新
    2020-11-27 05:47

    The answer suggested by dummyboy is the best answer (it should be voted to the top imo). It does have an issue that the designer doesn't like it (gives an error "Object null cannot be used as an accessor parameter for a PropertyPath) but there is a good workaround. The workaround is to define the item in a datatemplate and then set the template to a label or other content control. As an example, I was trying to add an Image like this

    
    

    But it kept giving me the same error. The solution was to create a label and use a data template to show my content

    
    

    This has its downsides but it seems to work pretty well for me.

提交回复
热议问题