Jump to: navigation, search

Difference between revisions of "EvalExpression"


(Example)
Line 14: Line 14:
 
=== Example ===
 
=== Example ===
 
The following example shows how to use the control with three parameters, where the first two are user input (Textbox ''Param0'' and ''Param1'') and the third one is a property from its DataContext (ViewModel). The evaluated ''Result'' is then bound to a Textblock for output in the UI.
 
The following example shows how to use the control with three parameters, where the first two are user input (Textbox ''Param0'' and ''Param1'') and the third one is a property from its DataContext (ViewModel). The evaluated ''Result'' is then bound to a Textblock for output in the UI.
 +
 
<source lang = "xml">
 
<source lang = "xml">
<StackPanel
+
<StackPanel xmlns:ctrls="using:UBIK.WinX.Controls"
 
HorizontalAlignment="Center"
 
HorizontalAlignment="Center"
 
Orientation="Vertical">
 
Orientation="Vertical">
Line 32: Line 33:
  
 
Lets assume that the Textbox ''Param0'' contains a text of 42 and ''Param1'' contains a text of 43. If ''Expression'' now contains  <code>(P0 + P1) *2</code> then the result would display 170.
 
Lets assume that the Textbox ''Param0'' contains a text of 42 and ''Param1'' contains a text of 43. If ''Expression'' now contains  <code>(P0 + P1) *2</code> then the result would display 170.
 +
 +
[[Category:Pages with broken file links|EvalExpression]]
  
 
== Parameters ==
 
== Parameters ==

Revision as of 10:46, 7 August 2019

EvalExpression
220px
imagecaption
Name EvalExpression
Namespace UBIK.WinX.Controls
Purpose Evaluate a C# expression in XAML
Version 3.2+

Usage

The EvalExpression control allows to evaluate a C# expression from within XAML markup. The Expression has to be a single-line, valid C# expression ("Lambda") and has to return a single value; expressions can also reference names of subordinate EvalExpressionParameter items.

Example

The following example shows how to use the control with three parameters, where the first two are user input (Textbox Param0 and Param1) and the third one is a property from its DataContext (ViewModel). The evaluated Result is then bound to a Textblock for output in the UI.

<StackPanel xmlns:ctrls="using:UBIK.WinX.Controls"
        HorizontalAlignment="Center"
        Orientation="Vertical">
        <TextBox x:Name="Expression" Width="200" />
        <TextBox x:Name="Param0" Width="200" />
        <TextBox x:Name="Param1" Width="200" />
        <ctrls:EvalExpression x:Name="Evaluator" Expression="{Binding ElementName=Expression, Path=Text}">
                <ctrls:EvalExpressionParameter Name="P0" Value="{Binding ElementName=Param0, Path=Text, Converter={StaticResource ToType}, ConverterParameter='System.Int32'}" />
                <ctrls:EvalExpressionParameter Name="P1" Value="{Binding ElementName=Param1, Path=Text, Converter={StaticResource ToType}, ConverterParameter='System.Int32'}" />
                <ctrls:EvalExpressionParameter Name="P2" Value="{Binding IsLoggedIn}" />
        </ctrls:EvalExpression>
        <TextBlock Foreground="White" Text="{Binding ElementName=Evaluator, Path=Result}" />
</StackPanel>


Lets assume that the Textbox Param0 contains a text of 42 and Param1 contains a text of 43. If Expression now contains (P0 + P1) *2 then the result would display 170.

Parameters

EvalExpressionParameters can be added as child objects to an EvalExpression control. Each parameter object needs a unique Name and a Value, where the latter can be either a constant or dynamic value supplied through a binding.

See also