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

8
我想做的只是将一个公共属性绑定到文本块上。我在这里做错了什么?
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个回答

15

您可以简单地添加一个数据上下文并访问您的属性。

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


8

首先,您需要让您的类实现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>

1
你实际上不需要实现INotifyPropertyChanged接口。然而,这将是一次性的数据绑定。
例如,在XAML中:
<TextBlock Name="SomeTextBlock" Text="{Binding Path=SomeProp}" />

在代码中:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接