How to databind public property in xaml

巧了我就是萌 提交于 2021-02-07 06:36:07

问题


All I am trying to do is bind a public property to a textBlock. What am I doing wrong here?

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>


回答1:


You can simply add a datacontext and access your property

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}"/>

Also check this post for details of when to use an ObjectDataProvider

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




回答2:


At first you need you class to implement INotifyPropertyChanged or a property to be DependencyProperty for changing property value on textbox text change,

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));
       }
    }
}

}

Than you can bind to that property by giving name to that window, and using ElementName property like this.

<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>



回答3:


You actually don't need to implement INotifyPropertyChanged. However, this will be a one time data binding.

For example in XAML:

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

In Code:

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


来源:https://stackoverflow.com/questions/4740217/how-to-databind-public-property-in-xaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!