ceth
ceth

Reputation: 45285

Bind class instance to controls

I am studying WPF databinding using this tutorial.

Here is my XAML:

Window x:Class="DataBinding_01.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>
        <local:Person x:Key="PersonDataSource" Name="Joe"/>
    </Window.Resources>

    <DockPanel Height="Auto" Name="panel" Width="Auto" LastChildFill="True">
        <TextBox DockPanel.Dock="Top" Height="23" Name="txtName" Width="Auto" />
        <Button Content="Button" Name="button1" Width="Auto" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Click="button1_Click" />
    </DockPanel>
</Window>

Here is my code:

 public partial class MainWindow : Window
    {
        Person myPerson = null;
        public MainWindow()
        {
            InitializeComponent();
            myPerson = this.Resources["PersonDataSource"] as Person;
            myPerson.NameProperty = "hi, again!";
        }    
    }

    public class Person 
    {

        Person()
        {
            NameProperty = "hi";
        }

        Person(String _name)
        {
            NameProperty = _name;
        }

        private String name = "";
        public String NameProperty
        {
            get { return name; }
            set 
            { 
                name = value;
            }
        }  

    }

When I am building the solution I get the error:

Error 1 ''local' is an undeclared prefix. Line 7, position 10.' XML is not valid. C:\Users\Admin\Desktop\DataBinding_01\DataBinding_01\MainWindow.xaml 7 10 DataBinding_01

Why and how can I fix it ?

Upvotes: 0

Views: 2765

Answers (2)

paparazzo
paparazzo

Reputation: 45096

You need to define local where myNameSpace is your name space in the window section. Usually the name of the project.

xmlns:local="clr-namespace:myNameSpace"

Upvotes: 2

nemesv
nemesv

Reputation: 139748

You need to specify the local namespace in XAML to point to your Person's class namespace:

If the Person class in the DataBinding_01 namespace:

<Window x:Class="DataBinding_01.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DataBinding_01"        
        Title="MainWindow" Height="350" Width="525">

In your sample article:

<Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:BindingSample">

Upvotes: 5

Related Questions