单值转换器

发布时间 2023-12-22 16:56:44作者: 时而有风

https://www.cnblogs.com/tianma3798/p/5927470.html

 

1.当值从绑定源(属性)传播给绑定目标(例XAML中的TextBox的Text)时,调用方法Convert

2.当值从绑定目标传播给绑定源时,调用此方法ConvertBack,方法ConvertBack的实现必须是方法Convert的反向实现。

以下例子为HTuple属性与XAML中的TextBox的Text绑定,由于HTuple类型无法使用INotifyPropertyChanged接口,并且无法进行HTuple和string类型的的绑定,故使用下例:

public class TimeConver : IValueConverter
{
    /// <summary> 转换值。 </summary>
    /// <param name = "value"> 绑定源生成的值。 </param>
    /// <param name = "targetType"> 绑定目标属性的类型。 </param>
    /// <param name = "parameter"> 要使用的转换器参数。 </param>
    /// <param name = "culture"> 要用在转换器中的区域性。 </param>
    /// <returns>
    /// 转换后的值。
    /// 如果该方法返回 <see langword = "null"/>,则使用有效的 null 值。
    /// </returns>
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
        {
            return DependencyProperty.UnsetValue;
        }

        return value.ToString();
    }

    /// <summary> 转换值。 </summary>
    /// <param name = "value"> 绑定目标生成的值。 </param>
    /// <param name = "targetType"> 要转换为的类型。 </param>
    /// <param name = "parameter"> 要使用的转换器参数。 </param>
    /// <param name = "culture"> 要用在转换器中的区域性。 </param>
    /// <returns>
    /// 转换后的值。
    /// 如果该方法返回 <see langword = "null"/>,则使用有效的 null 值。
    /// </returns>
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var str = value as string;
        return (HTuple) str;
        // return DependencyProperty.UnsetValue;
    }
}

注:返回值DependencyProperty.UnsetValue表示转换器没有生成任何值。

在xaml中引用TimeConver的命名空间:

xmlns:local="clr-namespace:DTJY4180.Main.Vision.Shape.Common"

在xaml中定义Resources:

    <UserControl.Resources>
        <local:TimeConver x:Key="Conver" />
    </UserControl.Resources>

在xaml中Binding值使用自定义Converter转换 :

  Converter={StaticResource Conver}}

<TextBox x:Name="NumLevelsBox" Margin="2.5" Height="20"
         Text="{Binding NumLevels,Converter={StaticResource Conver}}" />
<TextBox x:Name="StartAngleBox" Margin="2.5" Height="20"
         Text="{Binding AngleStart,Converter={StaticResource Conver}}" />
<TextBox x:Name="EndAngleBox" Margin="2.5" Height="20"
         Text="{Binding AngleExtent,Converter={StaticResource Conver}}" />
<TextBox x:Name="AngleStepBox" Margin="2.5" Height="20"
         Text="{Binding AngleStep,Converter={StaticResource Conver}}" />
<TextBox x:Name="OptimizationBox" Margin="2.5" Height="20"
         Text="{Binding Optimization,Converter={StaticResource Conver}}" />
<TextBox x:Name="MetricBox" Margin="2.5" Height="20"
         Text="{Binding Metric,Converter={StaticResource Conver}}" />
<TextBox x:Name="ContrastBox" Margin="2.5" Height="20"
         Text="{Binding Contrast,Converter={StaticResource Conver}}" />
<TextBox x:Name="MinContrastBox" Margin="2.5" Height="20"
         Text="{Binding MinContrast,Converter={StaticResource Conver}}" />

 某一个属性:

 

 

最后结果能实现HTuple和string属性的绑定:

  改变属性值:

   

  XAML中绑定的TextBox的Text随之改变