本文介绍了是否可以使用C#7绑定到WPF中的ValueTuple字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

如果我有viewmodel属性

If I have a viewmodel property

public (string Mdf, string MdfPath) MachineDefinition { get; set; }

,我尝试在XAML/WPF中将其绑定

and I try to bind to it in XAML / WPF

<Label Content="{Binding Path=MachineDefinition.Item2}" />

<Label  Content="{Binding Path=MachineDefinition.MdfPath}" />

我遇到同样的错误

我看到ValueTuple字段实际上是字段,而不是属性.这是问题吗?

I see that ValueTuple fields are really fields not properties. Is this the problem?

推荐答案

混乱之处在于,对于旧式元组(C#7之前的版本),所有项目都是属性

The confusion is that for old style Tuple ( pre C#7 ) all the Items were properties

https://msdn.microsoft.com/en-us/library/dd386940(v=vs.110).aspx

,因此具有约束力.对于ValueTuple,它们是字段

and thus bindable. For ValueTuple they are fields

https://github.com/dotnet/runtime/blob/5ee73c3452cae931d6be99e8f6b1cd47d22d69e8/src/libraries/System.Private.CoreLib/src/System/ValueTuple.cs#L269

且不可绑定.

如果您用Google搜索"WPF元组绑定" ,则会收到大量误报,因为旧式元组是可绑定的,而新式元组则不是.

If you google "WPF Tuple Binding" you get loads of false positives because old style tuples are bindable but the new ones are not.

这篇关于是否可以使用C#7绑定到WPF中的ValueTuple字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-09 02:28