TextBlock数据绑定

时间:2010-10-15 00:25:26

标签: c# wpf binding

当我运行以下代码时,文本块没有改变。

我的猜测是代码或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();
        }

    }
}

由于

1 个答案:

答案 0 :(得分:2)

将对象数据提供者更改为:

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