Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to databind public property in xaml

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>

like image 875
Morgan Herlocker Avatar asked Jan 19 '11 20:01

Morgan Herlocker


People also ask

What is DataContext in XAML?

The DataContext property is the default source of your bindings, unless you specifically declare another source, like we did in the previous chapter with the ElementName property. It's defined on the FrameworkElement class, which most UI controls, including the WPF Window, inherits from.

How do you use dependency property?

Custom Dependency Properties Follow the steps given below to define custom dependency property in C#. Declare and register your dependency property with system call register. Provide the setter and getter for the property. Define an instance handler which will handle any changes that occur to that particular instance.

What is binding XAML?

Advertisements. Data binding is a mechanism in XAML applications that provides a simple and easy way for Windows Runtime Apps using partial classes to display and interact with data. The management of data is entirely separated from the way the data is displayed in this mechanism.

What is databinding in WPF?

Data binding in Windows Presentation Foundation (WPF) provides a simple and consistent way for apps to present and interact with data. Elements can be bound to data from different kinds of data sources in the form of . NET objects and XML.


1 Answers

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

like image 110
biju Avatar answered Sep 18 '22 13:09

biju