是否可以使用 C#7 绑定到 WPF 中的 ValueTuple 字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43208852/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 14:03:39  来源:igfitidea点击:

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

c#wpfxamlvaluetuple

提问by bradgonesurfing

If I have a viewmodel property

如果我有一个 viewmodel 属性

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

and I try to bind to it in XAML / WPF

我尝试在 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 fieldsnot properties. Is this the problem?

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

回答by bradgonesurfing

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

令人困惑的是,对于旧式元组(C#7 之前),所有项都是属性

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

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

and thus bindable. For ValueTuple they are fields

因此可以绑定。对于 ValueTuple,它们是字段

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

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.

如果你在谷歌上搜索“WPF 元组绑定”,你会得到大量误报,因为旧式元组是可绑定的,而新式元组则不是。

回答by sergio

Something you could try is to implement a value converter. Here is an example...

您可以尝试实现一个值转换器。这是一个例子...

public class TupleDisplayNameConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var tuple = value as (Int32 Id, String Name)?;

        if (tuple == null)
            return null;

        return tuple.Value.Name;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}


<TextBlock Text="{Binding Converter={StaticResource TupleDisplayNameConverter}, Mode=OneWay}" />

Hope this helps.

希望这可以帮助。

回答by Marc Gravell

The MdfPathapproach will never work, since the name part is veryrestrictive in terms of where it actually exists. Essentially, it is pure compiler voodoo, and doesn't exist in the type model, which means that anything that talks to the type model (which includes reflection, UI tools, serializers, etc) will onlysee the Item1, Item2names; not the fake names.

这种MdfPath方法永远不会奏效,因为名称部分在它实际存在的位置方面非常严格。本质上,它是纯粹的编译器巫术,不存在于类型模型中,这意味着任何与类型模型(包括反射、UI 工具、序列化程序等)对话的东西都只会看到Item1,Item2名称;不是假名字。