简体   繁体   中英

How to keep the original value unchanged in MultiBinding

I have the following multi-binding for my TextBlock

<Multibinding Converter="{StaticResource myconv}">
 <Binding Path="Property1" />
 <Binding Path="Property2" />
 <Binding Path="Property3" />
</Multibinding>

Here is my converter code

public class PropertiesSelectorConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return values.Where(v => v != null).FirstOrDefault();

    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Problem Now what I would like to do here is when all of the Property1 , Property2 and Property3 are null, I want TextBlock to retain its original value. How do I accomplish this ?

You can use the special value of Binding called Binding.DoNothing :

public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    var value = values.Where(v => v != null).FirstOrDefault();
    return value == null ? Binding.DoNothing : value;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM