16

I want to add a constant value onto an incoming bound integer. In fact I have several places where I want to bind to the same source value but add different constants. So the ideal solution would be something like this...

<TextBox Canvas.Top="{Binding ElementName=mySource, Path=myInt, Constant=5}"/>
<TextBox Canvas.Top="{Binding ElementName=mySource, Path=myInt, Constant=8}"/>
<TextBox Canvas.Top="{Binding ElementName=mySource, Path=myInt, Constant=24}"/>

(NOTE: This is an example to show the idea, my actual binding scenario is not to the canvas property of a TextBox. But this shows the idea more clearly)

At the moment the only solution I can think of is to expose many different source properties each of which adds on a different constant to the same internal value. So I could do something like this...

<TextBox Canvas.Top="{Binding ElementName=mySource, Path=myIntPlus5}"/>
<TextBox Canvas.Top="{Binding ElementName=mySource, Path=myIntPlus8}"/>
<TextBox Canvas.Top="{Binding ElementName=mySource, Path=myIntPlus24}"/>

But this is pretty grim because in the future I might need to keep adding new properties for new constants. Also if I need to change the value added I need to go an alter the source object which is pretty naff.

There must be a more generic way than this? Any WPF experts got any ideas?

4

5 に答える 5

21

MathConverterすべての単純な算術演算を行うために作成した を使用します。コンバーターのコードはここにあり、次のように使用できます。

<TextBox Canvas.Top="{Binding SomeValue, 
             Converter={StaticResource MathConverter},
             ConverterParameter=@VALUE+5}" />

次のようなより高度な算術演算でも使用できます。

Width="{Binding ElementName=RootWindow, Path=ActualWidth,
                Converter={StaticResource MathConverter},
                ConverterParameter=((@VALUE-200)*.3)}"
于 2011-08-20T17:34:04.130 に答える
7

値コンバーターでこれを行うことができると思います。これは、xaml の値コンバーターにパラメーターを渡す方法を説明するブログ エントリです。このブログでは、値コンバーターの実装について詳しく説明しています。

于 2008-09-24T05:36:50.883 に答える
6

値コンバーターを使用すると、UIにバインドされているときにソース値を変更できるため、問題の良い解決策になります。

私はいくつかの場所で以下を使用しました。

public class AddValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        object result = value;
        int parameterValue;

        if (value != null && targetType == typeof(Int32) && 
            int.TryParse((string)parameter, 
            NumberStyles.Integer, culture, out parameterValue))
        {
            result = (int)value + (int)parameterValue;
        }

        return result;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

 <Setter Property="Grid.ColumnSpan"
         Value="{Binding 
                   Path=ColumnDefinitions.Count,
                   RelativeSource={RelativeSource AncestorType=Grid},
                   Converter={StaticResource addValueConverter},
                   ConverterParameter=1}"
  />
于 2008-09-24T10:56:31.740 に答える
0

私は WPF を使用したことがありませんが、可能な解決策があります。

バインディング パスをマップにマップできますか? もしそうなら、それは引数(キー)を取ることができるはずです。Map インターフェイスを実装するクラスを作成する必要がありますが、実際には、キーに追加して「Map」を初期化した基本値を返すだけです。

public Integer get( Integer key ) { return baseInt + key; }  // or some such

タグから数値を渡す機能がないと、元の値とは異なるデルタを返す方法がわかりません。

于 2008-09-24T05:40:34.057 に答える