如何在xaml中数据绑定公共属性

时间:2011-01-19 20:13:45

标签: wpf xaml public-key

我要做的就是将公共属性绑定到textBlock。我在这里做错了什么?

namespace WpfApplication1
{

    public partial class MainWindow : Window
    {

        public string test { get; set; }

        public MainWindow()
        {
            test = "this is a test";
            InitializeComponent();
        }
    }
}

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <ObjectDataProvider x:Key="test"></ObjectDataProvider>
</Window.Resources>
<Grid>
    <TextBlock Height="23" HorizontalAlignment="Left" Margin="108,58,0,0" Name="textBlock1"  VerticalAlignment="Top" Text="{Binding Source={StaticResource test}}" />
</Grid>

3 个答案:

答案 0 :(得分:11)

您只需添加datacontext并访问您的属性

即可
public partial class MainWindow : Window,INotifyPropertyChanged
{
    private string _test;
    public string test
    {
        get
        {
            return _test;
        }
        set
        {
            _test = value;
            OnPropertyChanged("test");
        }
    }
    public MainWindow()
    {
        test = "this is a test";
        InitializeComponent();
        DataContext = this;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(String name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="108,58,0,0" Name="textBlock1"  VerticalAlignment="Top" Text="{Binding test}"/>

另请查看此帖子,了解何时使用ObjectDataProvider

的详细信息

http://bea.stollnitz.com/blog/?p=22

答案 1 :(得分:7)

首先,您需要课程实施INotifyPropertyChanged或属性为DependencyProperty,以便更改文本框文本更改的属性值,

namespace WpfApplication1
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
    private string _test 
    public string test 
    { 
        get
        {
           return _test;
        } 
        set
        {
            _test = value;
            OnPropertyChanged("test");
        } 
    }

    public MainWindow()
    {
        test = "this is a test";
        InitializeComponent();
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(String info)
    {
       if (PropertyChanged != null)
       {
           PropertyChanged(this, new PropertyChangedEventArgs(info));
       }
    }
}

}

通过给该窗口命名,并使用像这样的ElementName属性,可以绑定到该属性。

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" Name="myWindow">
<Window.Resources>
    <ObjectDataProvider x:Key="test"></ObjectDataProvider>
</Window.Resources>
<Grid>
    <TextBlock Height="23" HorizontalAlignment="Left" Margin="108,58,0,0" Name="textBlock1"  VerticalAlignment="Top" Text="{Binding ElementName=myWindow, Path=test}" />
</Grid>

答案 2 :(得分:0)

您实际上不需要实现INotifyPropertyChanged。但是,这将是一次性数据绑定。

例如在XAML中:

<TextBlock Name="SomeTextBlock" Text="{Binding Path=SomeProp}" />

在代码中:

    public string SomeProp { get; set; }
    public MainWindow()
    {
        InitializeComponent();
        SomeProp = "Test Test Test";
        SomeTextBlock.DataContext = this;          
    }