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

前端 未结 3 767
慢半拍i
慢半拍i 2020-12-18 18:58

If I have a viewmodel property

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

and I try to bind to it in XAML / WPF

3条回答
  •  眼角桃花
    2020-12-18 19:51

    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();
        }
    }
    
    
    
    

    Hope this helps.

提交回复
热议问题