Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Math in XAML

Math in XAML

Scheduled Pinned Locked Moved WPF
csharpwpftutorial
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mc_Topaz
    wrote on last edited by
    #1

    I would like to do some simple math in my WPF application with as much XAML code as possible and with as little C# code as possible. I have looked at various examples how to do this on diffrent web pages, but they are either to complex or don't show all details or lacks code or is just a bad example. Please run my XAML code, just copy and paste it:

            <Setter Property="Control.HorizontalAlignment" Value="Left"></Setter>
            <Setter Property="Control.VerticalAlignment" Value="Top"></Setter>
        
        <Setter Property="Control.BorderBrush" Value="Black"></Setter>
            <Setter Property="Control.BorderThickness" Value="2"></Setter>
            <Setter Property="Control.FontSize" Value="15"></Setter>
        
        <Setter Property="Control.FontSize" Value="15"></Setter>
            <Setter Property="Control.FontWeight" Value="Bold"></Setter>
            <Setter Property="Control.Height" Value="30"></Setter>
    
    D P 2 Replies Last reply
    0
    • M Mc_Topaz

      I would like to do some simple math in my WPF application with as much XAML code as possible and with as little C# code as possible. I have looked at various examples how to do this on diffrent web pages, but they are either to complex or don't show all details or lacks code or is just a bad example. Please run my XAML code, just copy and paste it:

              <Setter Property="Control.HorizontalAlignment" Value="Left"></Setter>
              <Setter Property="Control.VerticalAlignment" Value="Top"></Setter>
          
          <Setter Property="Control.BorderBrush" Value="Black"></Setter>
              <Setter Property="Control.BorderThickness" Value="2"></Setter>
              <Setter Property="Control.FontSize" Value="15"></Setter>
          
          <Setter Property="Control.FontSize" Value="15"></Setter>
              <Setter Property="Control.FontWeight" Value="Bold"></Setter>
              <Setter Property="Control.Height" Value="30"></Setter>
      
      D Offline
      D Offline
      dasblinkenlight
      wrote on last edited by
      #2

      Short answer is "yes, it is possible". The long answer is a lot less pleasant: it can't be done for free. Pulling this trick requires work. In fact, it requires a lot of work. Here are the basic steps: 1. First, you need an expression library capable of evaluating expressions presented to it as strings. You can either build one (making a high-performance evaluation library on top of Linq expressions is surprisingly simple), or adopt one written by someone else (for example, this one: Expression Evaluator[^]). 2. Next, you will need to build a Converter class, which is a small adapter for using your expression evaluator in XAML. One class can implement both IMultiValueConverter.aspx[^] and IValueConverter[^], and expose a string property called "Source". 3. Import the assembly with your adapter into your XAML file by adding a clr-namespace referencing your assembly to the Windows element. xmlns:Converter="clr-namespace:MyNamespace.Converter;assembly=MyConverterAssembly" 4. Add a resource for each expression that you would like evaluated from your XAML:

      <Converter:ExpressionValueConverter
      x:Key="CalcTotalWeight"
      Source="(arg1 * 126) + (arg2 * 220)"
      />

      5. Use binding or multi-binding to attach your expression to labels and text boxes in your XAML:

      <MultiBinding Converter="{StaticResource CalcTotalWeigh}" FallbackValue="0">
      <Binding Path="txtA"/>
      <Binding Path="txtB"/>
      </MultiBinding>

      I went through this exercise once, and I can tell you that it is not easy. And I started with a well-tested expression library from step 1! But once you put it all together, it looks like a small miracle.

      M 1 Reply Last reply
      0
      • M Mc_Topaz

        I would like to do some simple math in my WPF application with as much XAML code as possible and with as little C# code as possible. I have looked at various examples how to do this on diffrent web pages, but they are either to complex or don't show all details or lacks code or is just a bad example. Please run my XAML code, just copy and paste it:

                <Setter Property="Control.HorizontalAlignment" Value="Left"></Setter>
                <Setter Property="Control.VerticalAlignment" Value="Top"></Setter>
            
            <Setter Property="Control.BorderBrush" Value="Black"></Setter>
                <Setter Property="Control.BorderThickness" Value="2"></Setter>
                <Setter Property="Control.FontSize" Value="15"></Setter>
            
            <Setter Property="Control.FontSize" Value="15"></Setter>
                <Setter Property="Control.FontWeight" Value="Bold"></Setter>
                <Setter Property="Control.Height" Value="30"></Setter>
        
        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        You could accomplish this entirely in your XAML by using inline code (it's not pretty, and it's not good practice, but it is possible). Basically, you would implement an <x:Code> section where you would put the code in place to handle the calculations, etc. The important thing to remember is that you should encode your code so that you have something that looks like this:<x:Code> <![CDATA[ // Do your work here ]]> </x:Code>

        Forgive your enemies - it messes with their heads

        My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

        M 1 Reply Last reply
        0
        • D dasblinkenlight

          Short answer is "yes, it is possible". The long answer is a lot less pleasant: it can't be done for free. Pulling this trick requires work. In fact, it requires a lot of work. Here are the basic steps: 1. First, you need an expression library capable of evaluating expressions presented to it as strings. You can either build one (making a high-performance evaluation library on top of Linq expressions is surprisingly simple), or adopt one written by someone else (for example, this one: Expression Evaluator[^]). 2. Next, you will need to build a Converter class, which is a small adapter for using your expression evaluator in XAML. One class can implement both IMultiValueConverter.aspx[^] and IValueConverter[^], and expose a string property called "Source". 3. Import the assembly with your adapter into your XAML file by adding a clr-namespace referencing your assembly to the Windows element. xmlns:Converter="clr-namespace:MyNamespace.Converter;assembly=MyConverterAssembly" 4. Add a resource for each expression that you would like evaluated from your XAML:

          <Converter:ExpressionValueConverter
          x:Key="CalcTotalWeight"
          Source="(arg1 * 126) + (arg2 * 220)"
          />

          5. Use binding or multi-binding to attach your expression to labels and text boxes in your XAML:

          <MultiBinding Converter="{StaticResource CalcTotalWeigh}" FallbackValue="0">
          <Binding Path="txtA"/>
          <Binding Path="txtB"/>
          </MultiBinding>

          I went through this exercise once, and I can tell you that it is not easy. And I started with a well-tested expression library from step 1! But once you put it all together, it looks like a small miracle.

          M Offline
          M Offline
          Mc_Topaz
          wrote on last edited by
          #4

          Thanks! Since I'm no master in XAML and I can practically do nothing if I don't have a simple and exact example, your guide will do me no good. I think I will skip this method and use the old fashion C# way instead. Ugly but easy...

          1 Reply Last reply
          0
          • P Pete OHanlon

            You could accomplish this entirely in your XAML by using inline code (it's not pretty, and it's not good practice, but it is possible). Basically, you would implement an <x:Code> section where you would put the code in place to handle the calculations, etc. The important thing to remember is that you should encode your code so that you have something that looks like this:<x:Code> <![CDATA[ // Do your work here ]]> </x:Code>

            Forgive your enemies - it messes with their heads

            My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

            M Offline
            M Offline
            Mc_Topaz
            wrote on last edited by
            #5

            Thanks! I tried that and it worked. But I don't like the method at all.

            S 1 Reply Last reply
            0
            • M Mc_Topaz

              Thanks! I tried that and it worked. But I don't like the method at all.

              S Offline
              S Offline
              SledgeHammer01
              wrote on last edited by
              #6

              Theres already one built for you. http://wpfconverters.codeplex.com/wikipage?title=User%20Documentation#ExpressionConverter[^]

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups