簡體   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