wpf 如何将 DataTemplate 数据类型绑定到接口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15023441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to bind DataTemplate datatype to interface?
提问by Charlie
I am writing a composite loosely coupled MVVM WPF application and child VMs in a parent VM are interfaces rather than class instances, e.g.
我正在编写一个复合松散耦合的 MVVM WPF 应用程序,父虚拟机中的子虚拟机是接口而不是类实例,例如
public IChildViewModel { get; set; }
Now how do I render this property using a DataTemplate? like:
现在如何使用 DataTemplate 呈现此属性?喜欢:
<DataTemplate DataType="{x:Type contracts:IChildViewModel}">
I understand due to the nature of interfaces (multiple inheritance etc.) WPF does not allow this direct binding. But as interfaces should be used widely in loosely coupled applications, is there any workaround to bind DataTemplate to interfaces? Thanks.
我了解由于接口(多重继承等)的性质,WPF 不允许这种直接绑定。但是由于接口应该广泛用于松散耦合的应用程序中,是否有任何解决方法可以将 DataTemplate 绑定到接口?谢谢。
回答by t0yk4t
You can bind to interfaces by telling wpf explicitly that you are binding to an interface field:
您可以通过明确告诉 wpf 您正在绑定到接口字段来绑定到接口:
(Please note that ViewModelBase is simply a base-class that implements the INotifyPropertyChanged interface)
(请注意 ViewModelBase 只是一个实现 INotifyPropertyChanged 接口的基类)
public class Implementation : ViewModelBase, IInterface
{
private string textField;
public string TextField
{
get
{
return textField;
}
set
{
if (value == textField) return;
textField = value;
OnPropertyChanged();
}
}
}
public interface IInterface
{
string TextField { get; set; }
}
Then on the ViewModel:
然后在 ViewModel 上:
private IInterface interfaceContent;
public IInterface InterfaceContent
{
get { return interfaceContent; }
}
And finally the Xaml that makes it possible:
最后是使之成为可能的 Xaml:
<ContentControl Grid.Row="1" Grid.Column="0" Content="{Binding InterfaceContent}">
<ContentControl.ContentTemplate>
<DataTemplate DataType="{x:Type viewModels:IInterface}">
<TextBox Text="{Binding Path=(viewModels:IInterface.TextField)}"/>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
As you can see, the binding refers explicitly to the 'IInterface' definiton.
如您所见,绑定明确引用了“IInterface”定义。
回答by Lamelas84
You can convert your interface to an equivalent abstract class. It works in this way.
您可以将接口转换为等效的抽象类。它以这种方式工作。
回答by Natxo
It seems that using a DataTemplateSelectoris the way to go in such situations.
DataTemplateSelector在这种情况下,似乎使用 a是一种方法。
回答by sjb-sjb
I have used Binding with interface types in a data template, in uwp. I did not specify the interface type explicitly on the Binding path. It worked when the interface was not implemented explicitly. When the interface was implemented explicitly it failed silently. I believe that if the interface is implemented explicitly then the explicit reference to the interface type in the Binding path is needed, so that the Binding can correctly look up the property path.
我在 uwp 的数据模板中使用了具有接口类型的绑定。我没有在绑定路径上明确指定接口类型。当接口没有明确实现时,它工作。当接口被显式实现时,它默默地失败了。我相信如果接口是显式实现的,那么需要在 Binding 路径中显式引用接口类型,这样 Binding 才能正确查找属性路径。

