Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create control from XAML in codebehind?

I have XAML defining something, say:

<Path Data="M16.25,11 L0.5,0.5 L32,0.5 z" Fill="#FFF4F4F5" Height="11" Margin="1.542,0.25,18.458,0" Stretch="Fill" Stroke="Black" UseLayoutRounding="False" VerticalAlignment="Top"/>

Can I do something like this in codebehind:

Path myPath = Path.FromXAML("
    <Path Data="M16.25,11 L0.5,0.5 L32,0.5 z" Fill="#FFF4F4F5" Height="11" Margin="1.542,0.25,18.458,0" Stretch="Fill" Stroke="Black" UseLayoutRounding="False" VerticalAlignment="Top"/>");

Obviously with appropriate escape characters to ensure the string parses properly.

I recall that this was possible but I can't remember the specifics..

like image 531
meds Avatar asked Sep 27 '26 08:09

meds


1 Answers

Just figured it out, using XamlReader.Load does it nicely.

One small modification I have to make to the code is add the namespace, so to convert this:

<Path Data="M16.25,11 L0.5,0.5 L32,0.5 z" Fill="#FFF4F4F5" Height="11" Margin="1.542,0.25,18.458,0" Stretch="Fill" Stroke="Black" UseLayoutRounding="False" VerticalAlignment="Top"/>

To run via XamlReader.Load I do:

Path p = XamlReader.Load(<Path **xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"**  Data="M16.25,11 L0.5,0.5 L32,0.5 z" Fill="#FFF4F4F5" Height="11" Margin="1.542,0.25,18.458,0" Stretch="Fill" Stroke="Black" UseLayoutRounding="False" VerticalAlignment="Top"/>);

(obviously would also have to add escape characters for quotation marks)

like image 74
meds Avatar answered Sep 29 '26 23:09

meds