本文介绍了你如何更改绑定值,扭转它,繁衍,从失败中加上或减去呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一;问题是修辞,我有一个答案!我从这里看,我想给这一技巧的背部得到这么多的帮助。

First; the question is rhetorical, I have an answer! I have gotten so much help from looking here that I wanted to give this neat trick back.

假设你有,你要绑定到一个值,但它是某种或者有点不对。

Imagine that you have a value that you want to bind to, but it is somehow or somewhat wrong.


  • 我在哪里,我想绑定到一个值的情况,但是当值1,我需要0,反之亦然。

  • 曾经有一段时间,我想一个元素的宽度绑定到父宽度的时间 - 68px。

推荐答案

输入在 FirstDegreeFunctionConverter

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace GenericWPF
{
    /// <summary>
    /// Will return a*value + b
    /// </summary>
    public class FirstDegreeFunctionConverter : IValueConverter
    {
        public double A { get; set; }
    public double B { get; set; }

    #region IValueConverter Members

    public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
    {
        double a = GetDoubleValue( parameter, A );

        double x = GetDoubleValue( value, 0.0 );

        return ( a * x ) + B;
    }

    public object ConvertBack( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
    {
        double a = GetDoubleValue( parameter, A );

        double y = GetDoubleValue( value, 0.0 );

        return ( y - B ) / a;
    }

    #endregion


    private double GetDoubleValue( object parameter, double defaultValue )
    {
        double a;
        if( parameter != null )
            try
            {
                a = System.Convert.ToDouble( parameter );
            }
            catch
            {
                a = defaultValue;
            }
        else
            a = defaultValue;
        return a;
    }
}

如何使用它呢?

How to use it?

您做出在资源部分中的每个使用资源:

You make a resource for each use in the resource section:

<GenericWPF:FirstDegreeFunctionConverter x:Key="ReverseOne"
                            A="-1"
                            B="1" />

<Border Opacity="{Binding Path=Opacity
    , ElementName=daOtherField
    , Converter={StaticResource ReverseOne}}" />

<GenericWPF:FirstDegreeFunctionConverter x:Key="ListboxItemWidthToErrorWidth"
     A="1"
     B="-68" />

<TextBox MaxWidth="{Binding Path=ActualWidth
   , Converter={StaticResource ListboxItemWidthToErrorWidth}
   , RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" />

这个名字来源于函数y = A * X + B(被称为挪威第一度函数),当然有可能将其升级到第二度函数Y = A * X ^ 2 + BX + C,但我还没有找到一个使用它呢。

The name comes from the function y = a*x + b (Called a "first degree function" in Norwegian), and of course it would be possible to upgrade it to a second degree function y= a*x^2 + bx + c, but I haven't found a use for it yet.

我有,我想使基于宽列的情况。每一次我得到了200多像素宽度,我希望容器以显示我的另一列。当时我很难codeD转换器,但是我已经做出了Y =(A / X)+ B,而不是转换

I had a situation where I wanted to make columns based on width. Each time I got 200 pixels more width, I wanted the container to show me another column. At that time I hardcoded a converter, but I should have made a y=(a/x) + b converter instead.

现在,我应该有一个名为此转换器,让大家明白这是什么?因为我是一个挪威人,我用前pression我们在学校里学到,直接翻译。请,如果您有建议或意见,让我知道。
你有没有想过任何改进或改进也将是AP preciated ...

Now, what should I have named this converter so that everybody understand what it is? Since I'm a Norwegian, I used the expression we learned in school, directly translated. Please, if you have a suggestion or an opinion, let me know.Any refinements or improvements you have thought of would also be appreciated...

也许 LinearTransformConverter 将更好地沟通一下转换器为你做的,我会考虑的。
任何其他建议?
Tor的

Maybe "LinearTransformConverter" would better communicate what the converter does for you, I'll think about it.Any other proposals?Tor

这篇关于你如何更改绑定值,扭转它,繁衍,从失败中加上或减去呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 10:55