Programming‎ > ‎WPF‎ > ‎

Scripts in XAML

This is a nice idea of exteding XAML with script language:
http://denisvuyka.wordpress.com/2010/05/19/dynamic-silverlight-forms-embedding-s-scripts-into-xaml/

This is for Silverlight. I'm copying it here, because I feel that someday I will want to do the same in WPF and I don't want to loose this example as reference. The implementation is suprisingly easy. Assuming that you have scripting language already (and that should be no problem at these times).

The XAML code can like this:

<UserControl.Resources>
    <scriptingRuntime:ScriptContext x:Key="defaultContext"/>
</UserControl.Resources>

<Button x:Name="button1" Content="Click Me" Width="100">
  <scriptingToolkit:ScriptManager.OnButtonClick>
    <scriptingToolkit:CodeBlock ExecutionContext="{StaticResource defaultContext}">
      <sys:String>
            MessageBox.Show("Button was clicked!");
                      textBox = Form.FindName("textBox");
                      textBox.Text = "Dynamically modified text";

      </sys:String>
    </scriptingToolkit:CodeBlock>
  </scriptingToolkit:ScriptManager.OnButtonClick>
</Button>

Of course it should be used wisely and it is not recommended for novice WPF programmers.

Another example: "Using IronPython in WPF to Evaluate Expressions" by Sacha Barber.

ċ
SLForms.zip
(30k)
Marek Ogarek,
Aug 7, 2012, 12:24 AM
Comments