繁体   English   中英

如何在不使用xaml的情况下在C#中绑定IValueConverter值

[英]How to bind IValueConverter value without using xaml in c#

我们通常按如下所示绑定IValueConverter值,

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

但是我的要求是,不使用xaml绑定IValueConverter。 请任何人建议我如何在不使用xaml的情况下使用c#绑定IValueConverter

您将在c#中使用它,就像使用任何其他类一样。

因此,如果您的价值转换器是这样的:

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                              
    }
}

您可以这样使用它:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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