How do I bind commands to a bound itemList in a Avalonia UI? - xaml

I created a menu in Avalonia UI. But I guess this would possible with all types of bound lists.
I have a stringlist defined in my ViewModel ( List<CaptureDeviceDescriptor?> DeviceList ) where the user can select the active webcam. But the command binding fails, as the items are not clickable.
What am I doing wrong? Below is the menu with binding. All other functionality seems to work just fine. Just this command binding ....
I defined the Command as:
SetWebCam = ReactiveCommand.Create<CaptureDeviceDescriptor?>(SetTheWebCam); The other definitions are like:
public ReactiveCommand<CaptureDeviceDescriptor?, Unit> SetWebCam { get; }
private void SetTheWebCam(CaptureDeviceDescriptor? e)
{
Console.WriteLine(e);
}
But I don't really understand what is missing.
<Menu Grid.Column="1" Height="32" Padding="0" Margin="0">
<Menu.Styles>
<Style Selector="MenuItem.deviceList MenuItem">
<Setter Property="Header" Value="{Binding Name}"/>
<Setter Property="Command" Value="{Binding Path=DataContext.SetWebCam, RelativeSource={RelativeSource AncestorType=Window}}" />
<Setter Property="CommandParameter" Value="{Binding Self}" />
</Style>
</Menu.Styles>
<MenuItem Height="32" Width="45">
<MenuItem.Header>
<Path
Margin="4"
HorizontalAlignment="Center"
Stretch="Uniform"
Stroke="Black"
StrokeThickness="0.5"
Fill="White" Data="M0,8 A1.778,-1.778 0 0 0 3.556,8 A1.778,-1.778 0 0 0 0,8 M0,1.778 A1.778,-1.778 0 0 0 3.556,1.778 A1.778,-1.778 0 0 0 0,1.778 M0,14.22 A1.778,-1.778 0 0 0 3.556,14.22 A1.778,-1.778 0 0 0 0,14.22" />
</MenuItem.Header>
<MenuItem Header="Use silhouette">
<MenuItem.Icon>
<Path
Stroke="Black"
StrokeThickness="0.5"
Stretch="Uniform"
Fill="White" Data="M4,4 A4,4 0 0 1 12,4 L12,5.6 A4,4 0 0 1 4,5.6 L4,4 z M0,13.34 A15.92,15.92 0 0 1 8,11.2 C10.91,11.2 13.65,11.98 16,13.34 L16,16 L0,16 L0,13.34 z" />
</MenuItem.Icon>
</MenuItem>
<Separator />
<MenuItem Header="Rotate 90 CW">
<MenuItem.Icon>
<Path
Stroke="Black"
StrokeThickness="0.5"
Stretch="Uniform"
Fill="White" Data="M1.524,0.7619 L3.81,0.7619 A1.524,1.524 0 0 1 5.333,2.286 L5.333,14.48 A1.524,1.524 0 0 1 3.81,16 L1.524,16 A1.524,1.524 0 0 1 0,14.48 L0,2.286 A1.524,1.524 0 0 1 1.524,0.7619 M13.71,10.67 A1.524,1.524 0 0 1 15.24,12.19 L15.24,14.48 A1.524,1.524 0 0 1 13.71,16 L6.857,16 L6.857,10.67 L13.71,10.67 M9.143,2.286 A6.095,6.095 0 0 1 15.24,8.381 L15.19,9.143 L13.65,9.143 L13.71,8.381 A4.571,4.571 0 0 0 9.143,3.81 L9.143,6.095 L6.095,3.048 L9.143,0 L9.143,2.286 z" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="Rotate 90 CCW">
<MenuItem.Icon>
<Path
Stroke="Black"
StrokeThickness="0.5"
Stretch="Uniform"
Fill="White" Data="M6.095,2.286 L6.095,0 L9.143,3.048 L6.095,6.095 L6.095,3.81 A4.571,4.571 0 0 0 1.524,8.381 L1.585,9.143 L0.04571,9.143 L0,8.381 A6.095,6.095 0 0 1 6.095,2.286 M11.43,0.7619 L13.71,0.7619 A1.524,1.524 0 0 1 15.24,2.286 L15.24,14.48 A1.524,1.524 0 0 1 13.71,16 L11.43,16 A1.524,1.524 0 0 1 9.905,14.48 L9.905,2.286 A1.524,1.524 0 0 1 11.43,0.7619 M1.524,10.67 L8.381,10.67 L8.381,16 L1.524,16 A1.524,1.524 0 0 1 0,14.48 L0,12.19 A1.524,1.524 0 0 1 1.524,10.67 z" />
</MenuItem.Icon>
</MenuItem>
<Separator />
<MenuItem Classes="deviceList"
Header="Choose webcam"
Items="{Binding DeviceList}" />
</MenuItem>
</Menu>

After a few debugging rounds (aka Trial and Error ;) ) I found I was binding to the wrong RelativeSource. This is working as a charm. Binding to the root element (in my case a Panel)
<Menu.Styles>
<Style Selector="MenuItem.deviceList MenuItem">
<Setter Property="Header" Value="{Binding Name}"/>
<Setter Property="Command" Value="{Binding Path=DataContext.SetWebCam, RelativeSource={RelativeSource AncestorType=Panel}}" />
<Setter Property="CommandParameter" Value="{Binding }" />
</Style>
</Menu.Styles>

Related

Convert XAML path to SVG

I have the following XAML path which I want to convert to SVG. Is there a way to do it?
<Path Stroke="{DynamicResource ForegroundColorBrush}" Canvas.Right="0" Width="186" Height="216" Stretch="Fill" StrokeThickness="3" StrokeStartLineCap="Round" StrokeEndLineCap="Round" StrokeLineJoin="Round" Data="F1 M 184.69,166.333L 173.338,166.333L 173.338,80.8143L 153.122,15.4178L 153.122,5.35669C 152.785,4.01523 152.448,2.67374 140.823,2.00302C 129.199,1.33228 106.287,1.33228 94.6631,2.00302C 83.0388,2.67374 82.7018,4.01523 82.3649,5.35669L 82.3649,15.4178L 62.1486,80.8143L 62.1486,166.333L 1.49997,166.333L 1.49997,206.577L 62.1486,206.577L 62.1486,211.607C 65.1811,212.613 68.2135,213.62 85.2288,214.123C 102.244,214.626 133.242,214.626 150.258,214.123C 167.273,213.62 170.305,212.613 173.338,211.607L 173.338,206.577L 184.7,206.5" RenderTransformOrigin="0.5,0.5">
<Path.RenderTransform>
<ScaleTransform ScaleY="1" ScaleX="-1"/>
</Path.RenderTransform>
</Path>

Xpath in SVG element

I Have problem with localizing element in SVG object. I need to get text from element which is highlighted on picture below. I tried to use full xpath:
//*[local-name()='g']//*[local-name()='g' and #id='g-node-54591']//*[local-name()='g'][3]//*[local-name()='text']
Selenium is returning no such element exception.
Here is my Selenium code:
#FindBy (xpath = "//*[local-name()='g']//*[local-name()='g' and #id='g-node-54591']//*[local-name()='g'][3]//*[local-name()='text']")
WebElement endAOnDiagram;
public void verifyViewingEndAOnDiagram (String endA){
String txt = endAOnDiagram.getText();
System.out.println(txt);
Assert.assertTrue(txt.contains(endA));
}
I've read couple posts about making xpath for SVG elements but every suggested solution didn't work for me. If someone have any ideas, please let me know.
HTML CODE
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" contentScriptType="text/ecmascript" zoomAndPan="magnify" contentStyleType="text/css" preserveAspectRatio="xMidYMid meet"
version="1.0" xmlns:ev="http://www.w3.org/2001/xml-events" style="overflow: hidden; ">
<g id="viewport-20201012130550949" class="svg-pan-zoom_viewport" transform="matrix(0.8648062527318625,0,0,0.8648062527318625,-353.70811161620713,364.16635435112175)" style="transform: matrix(0.864806, 0, 0, 0.864806, -353.708, 364.166);">
<style>
text {
font-size: 12px
}
</style>
<g id="g-node-54591">
<rect x="500" y="50" fill="transparent" width="452" stroke-dasharray="5,5" height="206" stroke="black" />
<g />
<g />
<g>
<text x="500" id="pop-label-10" y="40" cursor="pointer" fill="black">10110, OS1080</text><svg x="736" width="20" y="0" height="40">
<polyline fill="transparent" x="0" y="20" points="0,20 10,0 20,20" stroke="black" />
<rect fill="transparent" x="0" width="20" y="20" height="20" stroke="black" /></svg></g>
</g>
<g id="g-node-61">
<rect x="1254" y="50" fill="transparent" width="452" stroke-dasharray="5,5" height="206" stroke="black" />
<g />
<g />
<g>
<text x="1254" id="pop-label-29544" y="40" cursor="pointer" fill="black">45187, WARE2</text><svg x="1490" width="20" y="0" height="40">
<polyline fill="transparent" x="0" y="20" points="0,20 10,0 20,20" stroke="black" />
<rect fill="transparent" x="0" width="20" y="20" height="20" stroke="black" /></svg></g>
</g>
<polyline fill="none" id="fs-id-4161198" points="922,80 1284,80" stroke-linecap="round" stroke="#36454F" stroke-width="1" />
<g>
<rect x="1184" y="60" fill="transparent" width="50" rx="20" ry="20" height="40" stroke="black" /><text x="1209" id="cable-label-83" y="50" style="text-anchor: middle;" cursor="pointer" fill="black">Abp-Alc1</text>
<g><text style="text-anchor: end;" x="1174" y="75">1/1</text></g>
<g>
<ellipse rx="20" fill="transparent" ry="10" cx="1209" cy="80" stroke="#FF0000" />
</g>
</g>
<g>
<rect x="972" y="60" fill="transparent" width="50" rx="20" ry="20" height="40" stroke="black" /><text x="997" id="cable-label-83" y="50" style="text-anchor: middle;" cursor="pointer" fill="black">Abp-Alc1</text>
<g><text style="text-anchor: start;" x="1032" y="75">1/1</text></g>
<g>
<ellipse rx="20" fill="transparent" ry="10" cx="997" cy="80" stroke="#FF0000" />
</g>
</g>
</g>
<defs>
<style id="svg-pan-zoom-controls-styles" type="text/css">
.svg-pan-zoom-control {
cursor: pointer;
fill: black;
fill-opacity: 0.333;
}
.svg-pan-zoom-control:hover {
fill-opacity: 0.8;
}
.svg-pan-zoom-control-background {
fill: white;
fill-opacity: 0.5;
}
.svg-pan-zoom-control-background {
fill-opacity: 0.8;
}
</style>
</defs>
<g id="svg-pan-zoom-controls" transform="translate(1146 876) scale(0.75)" class="svg-pan-zoom-control">
<g id="svg-pan-zoom-zoom-in" transform="translate(30.5 5) scale(0.015)" class="svg-pan-zoom-control">
<rect x="0" y="0" width="1500" height="1400" class="svg-pan-zoom-control-background" />
<path
d="M1280 576v128q0 26 -19 45t-45 19h-320v320q0 26 -19 45t-45 19h-128q-26 0 -45 -19t-19 -45v-320h-320q-26 0 -45 -19t-19 -45v-128q0 -26 19 -45t45 -19h320v-320q0 -26 19 -45t45 -19h128q26 0 45 19t19 45v320h320q26 0 45 19t19 45zM1536 1120v-960 q0 -119 -84.5 -203.5t-203.5 -84.5h-960q-119 0 -203.5 84.5t-84.5 203.5v960q0 119 84.5 203.5t203.5 84.5h960q119 0 203.5 -84.5t84.5 -203.5z"
class="svg-pan-zoom-control-element" />
</g>
<g id="svg-pan-zoom-reset-pan-zoom" transform="translate(5 35) scale(0.4)" class="svg-pan-zoom-control">
<rect x="2" y="2" width="182" height="58" class="svg-pan-zoom-control-background" />
<path d="M33.051,20.632c-0.742-0.406-1.854-0.609-3.338-0.609h-7.969v9.281h7.769c1.543,0,2.701-0.188,3.473-0.562c1.365-0.656,2.048-1.953,2.048-3.891C35.032,22.757,34.372,21.351,33.051,20.632z" class="svg-pan-zoom-control-element" />
<path
d="M170.231,0.5H15.847C7.102,0.5,0.5,5.708,0.5,11.84v38.861C0.5,56.833,7.102,61.5,15.847,61.5h154.384c8.745,0,15.269-4.667,15.269-10.798V11.84C185.5,5.708,178.976,0.5,170.231,0.5z M42.837,48.569h-7.969c-0.219-0.766-0.375-1.383-0.469-1.852c-0.188-0.969-0.289-1.961-0.305-2.977l-0.047-3.211c-0.03-2.203-0.41-3.672-1.142-4.406c-0.732-0.734-2.103-1.102-4.113-1.102h-7.05v13.547h-7.055V14.022h16.524c2.361,0.047,4.178,0.344,5.45,0.891c1.272,0.547,2.351,1.352,3.234,2.414c0.731,0.875,1.31,1.844,1.737,2.906s0.64,2.273,0.64,3.633c0,1.641-0.414,3.254-1.242,4.84s-2.195,2.707-4.102,3.363c1.594,0.641,2.723,1.551,3.387,2.73s0.996,2.98,0.996,5.402v2.32c0,1.578,0.063,2.648,0.19,3.211c0.19,0.891,0.635,1.547,1.333,1.969V48.569z M75.579,48.569h-26.18V14.022h25.336v6.117H56.454v7.336h16.781v6H56.454v8.883h19.125V48.569z M104.497,46.331c-2.44,2.086-5.887,3.129-10.34,3.129c-4.548,0-8.125-1.027-10.731-3.082s-3.909-4.879-3.909-8.473h6.891c0.224,1.578,0.662,2.758,1.316,3.539c1.196,1.422,3.246,2.133,6.15,2.133c1.739,0,3.151-0.188,4.236-0.562c2.058-0.719,3.087-2.055,3.087-4.008c0-1.141-0.504-2.023-1.512-2.648c-1.008-0.609-2.607-1.148-4.796-1.617l-3.74-0.82c-3.676-0.812-6.201-1.695-7.576-2.648c-2.328-1.594-3.492-4.086-3.492-7.477c0-3.094,1.139-5.664,3.417-7.711s5.623-3.07,10.036-3.07c3.685,0,6.829,0.965,9.431,2.895c2.602,1.93,3.966,4.73,4.093,8.402h-6.938c-0.128-2.078-1.057-3.555-2.787-4.43c-1.154-0.578-2.587-0.867-4.301-0.867c-1.907,0-3.428,0.375-4.565,1.125c-1.138,0.75-1.706,1.797-1.706,3.141c0,1.234,0.561,2.156,1.682,2.766c0.721,0.406,2.25,0.883,4.589,1.43l6.063,1.43c2.657,0.625,4.648,1.461,5.975,2.508c2.059,1.625,3.089,3.977,3.089,7.055C108.157,41.624,106.937,44.245,104.497,46.331z M139.61,48.569h-26.18V14.022h25.336v6.117h-18.281v7.336h16.781v6h-16.781v8.883h19.125V48.569z M170.337,20.14h-10.336v28.43h-7.266V20.14h-10.383v-6.117h27.984V20.14z"
class="svg-pan-zoom-control-element" />
</g>
<g id="svg-pan-zoom-zoom-out" transform="translate(30.5 70) scale(0.015)" class="svg-pan-zoom-control">
<rect x="0" y="0" width="1500" height="1400" class="svg-pan-zoom-control-background" />
<path
d="M1280 576v128q0 26 -19 45t-45 19h-896q-26 0 -45 -19t-19 -45v-128q0 -26 19 -45t45 -19h896q26 0 45 19t19 45zM1536 1120v-960q0 -119 -84.5 -203.5t-203.5 -84.5h-960q-119 0 -203.5 84.5t-84.5 203.5v960q0 119 84.5 203.5t203.5 84.5h960q119 0 203.5 -84.5 t84.5 -203.5z"
class="svg-pan-zoom-control-element" />
</g>
</g>
</svg>
Thanks!

How to get content to fill up entire button in XAML?

I have a UWP application, where I want a path to be the content of a button. So far, this is what I have:
<Style TargetType="Button" x:Key="arrow">
<Setter Property="Content">
<Setter.Value>
<Path Height="10" Width="10" Stretch="Uniform" Fill="Black" Data="F1 M 92.362,56.1133C 93.8282,57.8717 92.9765,60.7721 90.4336,60.7721L 80.461,60.7721L 80.461,103.953C 80.461,105.441 79.22,106.682 77.7318,106.682L 55.6016,106.682C 54.1133,106.682 52.8718,105.441 52.8718,103.953L 52.8718,60.7721L 42.8997,60.7721C 40.3575,60.7721 39.5052,57.8717 40.972,56.1133L 64.7376,27.5612C 65.3184,26.8627 66.0052,26.6081 66.6667,26.6536C 67.3282,26.6081 68.0131,26.8627 68.5964,27.5612L 92.362,56.1133 Z M 66.6667,0C 103.484,0 133.333,29.8476 133.333,66.6667C 133.333,103.486 103.484,133.333 66.6667,133.333C 29.8483,133.333 0,103.486 0,66.6667C 0,29.8476 29.8483,0 66.6667,0 Z M 66.6667,122.667C 97.5938,122.667 122.667,97.5938 122.667,66.6667C 122.667,35.739 97.5938,10.6667 66.6667,10.6667C 35.7389,10.6667 10.6667,35.739 10.6667,66.6667C 10.6667,97.5938 35.7389,122.667 66.6667,122.667 Z "/>
</Setter.Value>
</Setter>
</Style>
And the button:
<Button Height="13" Width="13" Style="{StaticResource arrow}">
However, the path doesn't show. Further investigating reveals that the path gets cut off at smaller sizes and works as content on the larger sizes. I suspect it might have to do with some kind of content padding. How do I make the path fill up the button instead of getting cut off?
You have solved it yourself - it's a padding problem (Button has a default one - just change it to 0). I've tried like this and it should work:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.Resources>
<Style TargetType="Button" x:Key="arrow">
<Setter Property="Padding" Value="0"/>
<Setter Property="Content">
<Setter.Value>
<Path Height="10" Width="10" Stretch="Uniform" Fill="Black" Data="F1 M 92.362,56.1133C 93.8282,57.8717 92.9765,60.7721 90.4336,60.7721L 80.461,60.7721L 80.461,103.953C 80.461,105.441 79.22,106.682 77.7318,106.682L 55.6016,106.682C 54.1133,106.682 52.8718,105.441 52.8718,103.953L 52.8718,60.7721L 42.8997,60.7721C 40.3575,60.7721 39.5052,57.8717 40.972,56.1133L 64.7376,27.5612C 65.3184,26.8627 66.0052,26.6081 66.6667,26.6536C 67.3282,26.6081 68.0131,26.8627 68.5964,27.5612L 92.362,56.1133 Z M 66.6667,0C 103.484,0 133.333,29.8476 133.333,66.6667C 133.333,103.486 103.484,133.333 66.6667,133.333C 29.8483,133.333 0,103.486 0,66.6667C 0,29.8476 29.8483,0 66.6667,0 Z M 66.6667,122.667C 97.5938,122.667 122.667,97.5938 122.667,66.6667C 122.667,35.739 97.5938,10.6667 66.6667,10.6667C 35.7389,10.6667 10.6667,35.739 10.6667,66.6667C 10.6667,97.5938 35.7389,122.667 66.6667,122.667 Z "/>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<Button HorizontalAlignment="Center" Height="14" Width="14" Style="{StaticResource arrow}"/>
</Grid>

How can I center an XML graphic within my XAML?

I am new to XAML and I am trying to figure out some of the ways you position things. I have a graphic that started off as an Adobe Illustrator image which I then converted to code that renders fine as XAML. However, I can't seem to get it to display centered within the display area. Using horizontalalignment, I can center other things without issue, but for some reason, I can't seem to get this graphic to be centered. I'm hoping that I just missed something.
<Window x:Class="play.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<Viewbox xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Height="320">
<Canvas x:Name="Layer_1" Canvas.Left="0" Canvas.Top="0" Width="62" Height="22" HorizontalAlignment="Center" Background="#00AEEF">
<!--Unknown tag: metadata-->
<!--Unknown tag: sodipodi:namedview-->
<Canvas x:Name="g3" HorizontalAlignment="Center">
<Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="path5" Fill="White" Margin="0,0,0,0" HorizontalAlignment="Center">
<Path.Data>
<PathGeometry Figures="M16.733 8.245c-0.027-0.413-0.393-0.726-0.798-0.698c-0.413 0.027-0.726 0.385-0.698 0.798 c0.123 1.855-0.554 3.68-1.855 5.006c-1.199 1.225-2.783 1.899-4.46 1.899c0 0 0 0-0.001 0c-1.553 0-3.007-0.59-4.125-1.656 l1.177-1.177c0.28-0.279 0.081-0.758-0.314-0.758H2.38c-0.245 0-0.444 0.199-0.444 0.444v3.278c0 0.396 0.478 0.594 0.758 0.314 l1.041-1.041c1.401 1.35 3.231 2.096 5.185 2.096c0.001 0 0.001 0 0.002 0c2.082 0 4.047-0.835 5.53-2.349 C16.053 12.771 16.884 10.527 16.733 8.245z" FillRule="NonZero"/>
</Path.Data>
</Path>
<Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="path7" Fill="White" Margin="0,0,0,0">
<Path.Data>
<PathGeometry Figures="M2.075 10.503c0.413-0.031 0.723-0.391 0.692-0.804c-0.14-1.868 0.535-3.709 1.85-5.051 C5.819 3.424 7.403 2.75 9.079 2.75c1.594 0 3.085 0.618 4.213 1.736l-1.152 1.152c-0.28 0.28-0.082 0.759 0.314 0.759h3.278 c0.245 0 0.444-0.199 0.444-0.444V2.674c0-0.396-0.479-0.594-0.758-0.314l-1.066 1.065C12.941 2.024 11.074 1.25 9.079 1.25 c-2.083 0-4.048 0.834-5.533 2.348C1.93 5.248 1.101 7.513 1.272 9.811c0.029 0.395 0.358 0.694 0.747 0.694 C2.037 10.505 2.057 10.504 2.075 10.503z" FillRule="NonZero"/>
</Path.Data>
</Path>
</Canvas>
</Canvas>
</Viewbox>
</StackPanel>
Canvas is only good for static placement of graphics.
You should start using grids and stackpanels.
I have modified your xaml:
<StackPanel>
<Viewbox xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Height="320">
<Grid x:Name="Layer_1" Width="62" Height="22" HorizontalAlignment="Center" Background="#00AEEF">
<!--Unknown tag: metadata-->
<!--Unknown tag: sodipodi:namedview-->
<Grid x:Name="g3" HorizontalAlignment="Center" VerticalAlignment="Center">
<Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="path5" Fill="White" Margin="0,0,0,0" HorizontalAlignment="Center">
<Path.Data>
<PathGeometry Figures="M16.733 8.245c-0.027-0.413-0.393-0.726-0.798-0.698c-0.413 0.027-0.726 0.385-0.698 0.798 c0.123 1.855-0.554 3.68-1.855 5.006c-1.199 1.225-2.783 1.899-4.46 1.899c0 0 0 0-0.001 0c-1.553 0-3.007-0.59-4.125-1.656 l1.177-1.177c0.28-0.279 0.081-0.758-0.314-0.758H2.38c-0.245 0-0.444 0.199-0.444 0.444v3.278c0 0.396 0.478 0.594 0.758 0.314 l1.041-1.041c1.401 1.35 3.231 2.096 5.185 2.096c0.001 0 0.001 0 0.002 0c2.082 0 4.047-0.835 5.53-2.349 C16.053 12.771 16.884 10.527 16.733 8.245z" FillRule="NonZero"/>
</Path.Data>
</Path>
<Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="path7" Fill="White" Margin="0,0,0,0">
<Path.Data>
<PathGeometry Figures="M2.075 10.503c0.413-0.031 0.723-0.391 0.692-0.804c-0.14-1.868 0.535-3.709 1.85-5.051 C5.819 3.424 7.403 2.75 9.079 2.75c1.594 0 3.085 0.618 4.213 1.736l-1.152 1.152c-0.28 0.28-0.082 0.759 0.314 0.759h3.278 c0.245 0 0.444-0.199 0.444-0.444V2.674c0-0.396-0.479-0.594-0.758-0.314l-1.066 1.065C12.941 2.024 11.074 1.25 9.079 1.25 c-2.083 0-4.048 0.834-5.533 2.348C1.93 5.248 1.101 7.513 1.272 9.811c0.029 0.395 0.358 0.694 0.747 0.694 C2.037 10.505 2.057 10.504 2.075 10.503z" FillRule="NonZero"/>
</Path.Data>
</Path>
</Grid>
</Grid>
</Viewbox>
</StackPanel>
maybe you should use a gird (Grid.RowDefinitions and Grid.ColumnDefinitions) to center your canvas instead of the HorizontalAlignment property
best of luck !

Bind a Path Stroke color to Foreground

Using the TabControl element for Silverlight in Blend I created the following markup:
<controls:TabControl>
<controls:TabItem Header="TabItem" Style="{StaticResource TabItemStyle1}" />
<controls:TabItem Style="{StaticResource TabItemStyle1}">
<controls:TabItem.Header>
<StackPanel Orientation="Horizontal">
<Path Data="M0,14L0,6 5,0 10,6 10,14 0,6 10,6 0,14 10,14"
StrokeLineJoin="Round" Margin="0 0 6 0"
Stroke="Black"/>
<TextBlock Text="TabItem"/>
</StackPanel>
</controls:TabItem.Header>
</controls:TabItem>
</controls:TabControl>
TabItemStyle1 is a copy of the default style of a TabItem.
I altered TabItemStyle1 by adding a color animation in the MouseOver storyboard so that unselected tab items become red when the mouse hovers them:
<ColorAnimation BeginTime="0" Duration="00:00:00.001"
Storyboard.TargetName="HeaderTopUnselected"
Storyboard.TargetProperty="(UIElement.Foreground).(SolidColorBrush.Color)"
To="Red" />
Now when I hover the second tab, the text becomes red but the Path remains black:
How should I define the Path Stroke color to make it follow the same rule?
The following should work:
<controls:TabControl>
<controls:TabItem Header="TabItem" Style="{StaticResource TabItemStyle1}" />
<controls:TabItem Style="{StaticResource TabItemStyle1}">
<controls:TabItem.Header>
<StackPanel Orientation="Horizontal">
<Path Data="M0,14L0,6 5,0 10,6 10,14 0,6 10,6 0,14 10,14"
StrokeLineJoin="Round" Margin="0 0 6 0"
Stroke="{Binding ElementName=textBlock, Path=Foreground}"/>
<TextBlock x:Name="textBlock" Text="TabItem"/>
</StackPanel>
</controls:TabItem.Header>
</controls:TabItem>
</controls:TabControl>
it's not a perfect solution but you could use this
<sdk:TabControl>
<sdk:TabItem Header="item1"></sdk:TabItem>
<sdk:TabItem Foreground="Red" x:Name="someNameForTheTab">
<sdk:TabItem.Header>
<StackPanel Orientation="Horizontal">
<!--Just set stroke binding to the foreground of the tabItem-->
<Path Stroke="{Binding Foreground, ElementName=someNameForTheTab}" Data="M0,14L0,6 5,0 10,6 10,14 0,6 10,6 0,14 10,14"
StrokeLineJoin="Round" Margin="0 0 6 0"/>
<TextBlock Text="item2"/>
</StackPanel>
</sdk:TabItem.Header>
</sdk:TabItem>
</sdk:TabControl>
Try binding to the TemplatedParent like this:
<Path
Data="M0,14L0,6 5,0 10,6 10,14 0,6 10,6 0,14 10,14"
StrokeLineJoin="Round"
Margin="0 0 6 0"
Stroke="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}"/>
I haven't tested this, but give it a whirl and let me know. If it doesn't work, try this:
<Path Data="M0,14L0,6 5,0 10,6 10,14 0,6 10,6 0,14 10,14" StrokeLineJoin="Round" Margin="0 0 6 0">
<Path.Stroke>
<SolidColorBrush Color="{Binding Foreground.Color, RelativeSource={RelativeSource TemplatedParent}}" />
</Path.Stroke>
</Path>
I have a feeling that the Color property needs to be the source of binding, not the actual brush.
I made it work by binding the header content brushes to {TemplateBinding TextElement.Foreground}.
In other cases I used standard property binding with converters, for example if I had to adapt element's brushes to item state.
// animazione periferica
public static void LineAnimation(Line _line,String _colore)
{
Storyboard result = new Storyboard();
Duration duration = new Duration(TimeSpan.FromSeconds(2));
ColorAnimation animation = new ColorAnimation();
animation.RepeatBehavior = RepeatBehavior.Forever;
animation.Duration = duration;
switch (_colore.ToUpper())
{
case "RED":
animation.From = Colors.Red;
break;
case "ORANGE":
animation.From = Colors.Orange;
break;
case "YELLOW":
animation.From = Colors.Yellow;
break;
case "GRAY":
animation.From = Colors.DarkGray;
break;
default:
animation.From = Colors.Green;
break;
}
animation.To = Colors.Gray;
Storyboard.SetTarget(animation, _line);
Storyboard.SetTargetProperty(animation, new PropertyPath("(Line.Stroke).(SolidColorBrush.Color)"));
result.Children.Add(animation);
result.Begin();
}
}
//**********************************************
public partial class MainPage : UserControl
{
public Line _line;
public MainPage()
{
InitializeComponent();
Canvas.MouseLeftButtonDown += Canvas_MouseLeftButtonDown;
Canvas.MouseLeftButtonUp += Canvas_MouseLeftButtonUp;
}
void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
_line.X2 = e.GetPosition(this.Canvas).X;
_line.Y2 = e.GetPosition(this.Canvas).Y;
_line.Loaded += _line_Loaded;
Canvas.Children.Add(_line);
}
void _line_Loaded(object sender, RoutedEventArgs e)
{
Cls_Barriere.LineAnimation(sender as Line, "RED");
}
void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_line = new Line();
_line.Stroke = new SolidColorBrush(Colors.White);
_line.StrokeThickness = 5;
_line.StrokeStartLineCap = PenLineCap.Round;
_line.StrokeEndLineCap = PenLineCap.Round;
_line.StrokeDashCap = PenLineCap.Round;
_line.X1 = e.GetPosition(this.Canvas).X;
_line.Y1= e.GetPosition(this.Canvas).Y;
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
}
}