Reputation: 303
I have a canvas defined in UserControl and I want to access GetLeft
method which is static. The problem is that this method requires static field... I tried to make a static constructor, but didn't helped... Any solution? I attached the code below.
<UserControl x:Class="DiagramDesigner.Resources.DesignerCanvas"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Canvas x:Name="CanvasX" Background="Transparent" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
Code:
CanvasX.GetLeft(new DesignerItem());
Error:
Cannot acces static method 'GetLeft' in non static context
Upvotes: 0
Views: 315
Reputation: 29073
The correct syntax is Canvas.GetLeft( designerItem )
GetLeft is a static method you pass a child element into - this method does not care which particular canvas the child is contained in.
Upvotes: 2
Reputation: 2781
To access static members you should specify the declaring Type
instead a variable.
From MSDN:
The static member is callable on a class even when no instance of the class has been created. The static member is always accessed by the class name, not the instance name.
So the correct syntax is:
double left = Canvas.GetLeft(designedItem);
Upvotes: 2