2013-05-15 40 views
1

考慮下面的例子設置Binding.Path拋出的一些機器異常

public class Test 
{ 
    private static string _property = "Success"; 
    public static string Property 
    { 
     get { return _property; } 
     set { _property = value; } 
    } 


    public void Check() 
    { 
     var prop = new PropertyPath(this.GetType().GetProperty("Property")); 
     var binding = new Binding(); 
     binding.Source = typeof(Test); 
     binding.Path = prop; 
    } 

    public static void DoTest() 
    { 
     new Test().Check(); 
    } 
} 

當我調用Test.DoTest()它工作正常在我的機器上,但拋出InvalidOperationException包含「消息無法分配Binding.StaticSource當使用Binding.Source 「(這不是確切的,翻譯文本)在其他一些機器上。如果屬性不是靜態的,則一切正常。什麼可能導致這種行爲?

+0

如果您希望綁定到靜態屬性,如果您的目標是未實現此目標的Framework 4.0? – LPL

+0

@LPL好吧,它只適用於安裝了.net 4.0的機器。其實我只需要OneWayToSource綁定。 – Poma

回答

0

我猜你在測試機器有不同的framework版本和你正在運行到,因爲在.NET 4.5這個新功能的不兼容性:http://msdn.microsoft.com/en-us/library/bb613588%28v=VS.110%29.aspx#static_properties

+0

但是我的目標框架是4.0 ... – Poma

+1

4.5是CLR的就地升級,因此即使在安裝了4.5的計算機上運行4.0應用程序也會對4.5 CLR運行,並且行爲可能會稍有不同。不確定這是否是您的問題,但應該是您檢查的第一件事。 –

1

3年前我曾WPF。 ..我記得所有這些,但使用這可能工作。

public class Test : DependencyObject 
{ 

    public static readonly DependencyProperty FilterStringProperty = 
     DependencyProperty.Register("Property", typeof(string), 
     typeof(Test), new UIPropertyMetadata("Success")); 
    public string Property 
    { 
     get { return (string)GetValue(FilterStringProperty); } 
     set { SetValue(FilterStringProperty, value); } 
    } 

    public static Test Instance { get; private set; } 

    static Test() 
    { 

    } 

    public void Check() 
    { 
     var prop = new PropertyPath(this.GetType().GetProperty("Property")); 

     var binding = new Binding(); 
     binding.Source = this; 
     //binding.Source = typeof(Test); //-- same thing 
     binding.Path = prop; 

    } 

    public static void DoTest() 
    { 

     Instance = new Test(); 
     new Test().Check(); 
    } 
}