2015-04-17 82 views
0

我在UserControl中定義了一個畫布,我想訪問GetLeft靜態方法。問題是,這種方法需要靜態字段...我試圖做一個靜態構造函數,但沒有幫助...任何解決方案?我附上了下面的代碼。WPF Canvas無法訪問GetLeft方法(靜態方法)

<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"> 

代碼:

CanvasX.GetLeft(new DesignerItem()); 

錯誤:

Cannot acces static method 'GetLeft' in non static context

回答

2

要訪問您應該指定宣佈Type,而不是一個變量的靜態成員。

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.

所以,正確的語法是:

double left = Canvas.GetLeft(designedItem); 
2

正確的語法是Canvas.GetLeft(designerItem)

GetLeft是你通過一個子元素到一個靜態方法 - 這種方法不關心兒童被包含在哪個特定畫布中。