如何对XAML绑定值执行计算:将其取反,相乘,相减或相加? [英] How perform calculation on a XAML Binding value: reverse it, multiply it, subtract from it or add to it?

查看:476
本文介绍了如何对XAML绑定值执行计算:将其取反,相乘,相减或相加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先;问题是修辞,我有答案!看这里我得到了很多帮助,我想把这个巧妙的技巧还给我.

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

如何使用?

您可以在资源部分为每次使用创建资源:

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像素时,我都希望容器向我显示另一列.那时我对转换器进行了硬编码,但我应该改为使用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.

现在,我应该给这个转换器起什么名字,以便每个人都知道它是什么?由于我是挪威人,因此我使用了我们在学校学到的表达,并直接进行了翻译.请,如果您有任何建议或意见,请告诉我. 您想过的任何改进或改进也将不胜感激...

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 "会更好地传达转换器为您所做的事情,我会考虑的. 还有其他建议吗? 托尔

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

这篇关于如何对XAML绑定值执行计算:将其取反,相乘,相减或相加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆