ValueTuple With WPF Binding [duplicate]
Asked Answered
S

1

5

why binding to ValueTuple property members (like Item1, Item2 ect) dont work?

<TextBlock x:Name="txtTest" Text="{Binding Item1}" />

the code:

txtTest.DataContext = ("Item A", "Another Item..");

output window:

BindingExpression path error: 'Item1' property not found on 'object' ''ValueTuple`2'

However in Tuple It always worked.

Schalles answered 15/6, 2017 at 11:5 Comment(2)
this.DataContext = new Tuple<string, string>("item 1", "item 2"); this is working fine.Highflier
@Highflier Right. I already mentioned this in my question.Schalles
S
13

As stated in the documentation, Item1 and Item2 of a ValueTuple are fields rather than properties and you can only bind to public properties in WPF.

So if you want to be able to bind to a tuple, you should use the Tuple class.

Sensationalism answered 15/6, 2017 at 11:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.