Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
674 views
in Technique[技术] by (71.8m points)

c# - Is it possible to bind to a ValueTuple field in WPF with C#7

If I have a viewmodel property

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

and I try to bind to it in XAML / WPF

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

or

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

I get the same error

enter image description here

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

and not bindable.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...