0

Good afternoon. My name is Eugene. First of all I bag your pardon for my poor English. I have the following question. In WPF-application main window I have a TextBox and in XAML markup I describe it as the follow:

<TextBox Name="tbxRange" Grid.Row="1" Grid.Column="3" TextAlignment="Center"
         Text="{Binding Path=CurCompassRange.Range, UpdateSourceTrigger=PropertyChanged,
         Mode=OneWayToSource, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>

In this TextBox I print a floating point number value. The contents of the Text property can be changed many times pending program execution. In the MainWindow class I have the following members as a fields:

private static Double topMargin = 0;
private static Double bottomMargin = 0;
private static Double initialValue;

The value to "initialValue" assigned once when application begins to run. I'm in need of the following. When I print a new number in TextBox its value first must be subtracted from "initialValue" (the value of "initialValue" itself doesn't change) and the result of subtraction must be stored in "bottomMargin" and secondly the value of printed number must be added to "initialValue" and the result of addition must be stored in "topMargin". I can change the contents of Text property of TextBox many times pending program execution. My question is the following. Can I write in XAML markup some triggers which can do above mentioned subtraction and addition each time the value of TextBox Text property changes? Of course I know that I can try to write (on C#) TextBox TextInput event handler to do these operations and do subtraction and addition in its body but to use triggers in XAML markup seems me more gracefuly. I'll be very appreciative to your help. I'm not very strong in WPF. Please help me.

user1688773
  • 157
  • 1
  • 3
  • 11

1 Answers1

0

First of all you cannot dynamically change Margin.Top or Margin.Bottom as "Top" or "Bottom" themselves are not a dependency properties. However, Margin itself is a dependency property which receive an instance of the "Thickness" type. Shortly put, this means that even if you intended to change Margin.Top alone you would have to apply a new Margin..

Second, the various calculations you required to do are not things you can do by just using XAML, you will have to create a value-converter in your code behind and relate your XAML binding to use that Value Converter.

So the simple answer is no - you cannot do what you described in XAML in its pure form, you will have to write some Code-Behind one way or another.

Félix LD
  • 372
  • 1
  • 5
  • 19
G.Y
  • 6,042
  • 2
  • 37
  • 54