简体   繁体   中英

How to bind IValueConverter value without using xaml in c#

We are normally bind the IValueConverter value like as below,

<Button x:Name="myButton" Content="ClickMe" />
<Image Opacity="{Binding ElementName=myButton, Path=IsEnabled, Converter={StaticResource BoolToOpacityConverter}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, ConverterParameter = 1}" />

But my requirement is, without using xaml bind the IValueConverter. Please anyone suggest me how to bind the IValueConverter using c# without using xaml

You would use it in c# exactly the same as if you were using any other class.

So if your value converter is like this:

public class CustomConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        // ... your custom value converter                    
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
         // ... your custom value converter                              
    }
}

you would use it like this:

var customConverter = new CustomConverter();
customConverter.Convert( .. );

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