1

I defined an object which contains properties in code-behind and, want to set the object to datacontext in xaml.

namespace WpfApplication4
{

    public partial class MainWindow : Window
    {
        public EquipInfo equip1;

        public MainWindow()
        {
            InitializeComponent();

            equip1 = new EquipInfo();
            //  setting here works fine but i want to do in xaml
            //textBox1.DataContext = equip1; 
        }
    }
}

here's xaml code..

<Window x:Class="WpfApplication4.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">
    <Grid>
        <TextBox Text="{Binding Path=PIN}" Height="23" 
                 HorizontalAlignment="Left" Margin="172,208,0,0" 
                 Name="textBox1" VerticalAlignment="Top" Width="120" />
    </Grid>
</Window>
Jin. K
  • 141
  • 2
  • 10

3 Answers3

3

You can set the DataContext via XAML to a public property. blindmeis has given a good example on how to do this.

What you did in your Post was not setting the DataContext, but binding the Text Property of your Control to a Property of the window. This only works if a DataContext is set. Another way to do that, if you don't want to do MVVM (which I'd recommend, though), is referencing specific Elements in your Binding:

1.) Alter member to AutoProperty or similar

public EquipInfo equip1 {get;set;}

2.) Give your window a name

<Window x:Class="WpfApplication4.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="myWin"
    Title="MainWindow" Height="350" Width="525">

2.) Access Window Properties by instance name

<TextBox Text="{Binding ElementName=myWin, Path=equip1.PIN}"/>

On a side note, it would really be better to use a MVVM instead.

Sebastian Edelmeier
  • 4,095
  • 3
  • 39
  • 60
  • Thanks for your answer. I didn't know the object had to be property and Path could take dot notation. However, although EquipInfo inherits INotifyPropertyChanged interface, changing PIN property does not result in Text box, and I had to instantiate EquipInfo before InitializeComponent() to see binding works. Any problem? – Jin. K May 09 '12 at 06:56
1

when you create your equipinfo class in codebehind why not setting the datacontext there?

nevertheless you can create a datacontext in xaml (you did not need the codebehind stuff then)

<TextBox Text="{Binding Path=PIN}">
     <TextBox.DataContext>
         <local:equip1/>
      </TextBox.DataContext>
 </TextBox>

but please look at the MVVM pattern

Sebastian Edelmeier
  • 4,095
  • 3
  • 39
  • 60
blindmeis
  • 22,175
  • 7
  • 55
  • 74
1

When you create your equipinfo class in codebehind why not setting the datacontext there?

The whole point of MVVM is to seperate UI from code behind using a viewmodel proxy. If you are going to reference the ui element to set its datacontext you might just as well reference it to set its value and forget about mvvm.

mishik
  • 9,973
  • 9
  • 45
  • 67
Ray
  • 11
  • 1