2010-10-15 44 views
0

當我運行下面的代碼文本塊不會改變。TextBlock Databinding

我的猜測是在代碼或XMAL中缺少數據綁定。

XAML

<Window.Resources> 
     <ObjectDataProvider x:Key="PersonObj" ObjectType="{x:Type local:Person}" MethodName="GetFirstName" /> 
    </Window.Resources> 

    <Grid> 
     <!--<TextBlock Margin="26,7,12,0" Name="myTextBlock" Text="{Binding Path=FirstName}" Height="69" VerticalAlignment="Top" />--> 
     <TextBlock Margin="26,7,12,0" Name="myTextBlock" Text="{Binding FirstName, Source={StaticResource PersonObj}}" Height="69" VerticalAlignment="Top" /> 
    </Grid> 
</Window> 

CODE

namespace WpfApplication1 
{ 

    public partial class Window1 : Window 
    { 
     public Window1() 
     { 
      InitializeComponent(); 
      test t = new test(); 
     } 
     public void testing() 
     { 
      test t = new test(); 
     }  
    } 

    public class Person 
    { 
     public String FirstName { get; set; } 
     public String LastName { get; set; } 

     public string GetFirstName() 
     { 
      return FirstName; 
     } 
    } 
} 

namespace WpfApplication1 
{ 
    class test : Person 
    { 
     public test() 
     { 
       this.FirstName = "John"; 
       this.LastName = "S"; 
       this.GetFirstName(); 
     } 

    } 
} 

由於

回答

2

變化對象數據提供者:

<ObjectDataProvider x:Key="PersonObj" ObjectType="{x:Type local:test}" /> 
+0

所以你必須使用ObjectType =「{x:Type local:ClassName}」。 myTextBlock是錯誤信息,我怎樣才能在多個類中使用它。如果在測試類中出現表達式,它將在主表單上顯示,如果在test1類中出現了異常,它將覆蓋它,如果有的話。 – juniorCSharp 2010-10-15 03:26:53

+0

我不知道我在追蹤@ _ @。但是如果我知道了,那麼我認爲你會在你的測試課中調用test1不是嗎?那麼你可以重寫測試類綁定屬性中的值 – dnr3 2010-10-15 05:18:32