Andrew Simpson
Andrew Simpson

Reputation: 7334

Binding textbox values to a model in wpf

I have a wpf app.

I have successfully bounded listviews to ObservableCollections.

I now want to do the same thing with a textbox and I have just a standard class model (no collection).

So, i have a static class:

namespace MyNameSpace
{
    public Static Class MyClass
    {
        public string MyField {get; set;}
    }
}  

and in my markup:

<TextBox Text="{Binding Path=MyNameSpace.MyClass.MyField}/>

But I get a run-time error to this declaration in my markup?

ADDITIONAL: In response to the answer this is the code used (but it does not work)

<Window x:Class="WpfApplication1.MainWindow"
        xmlns:vms="clr-namespace:WpfApplication1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <vms:Model></vms:Model>
    </Window.DataContext>
    <Grid>
        <StackPanel Orientation="Vertical">
        <TextBox Text="{Binding Path='MyField'}"/>
          <Button Content="Click Me!" Click="Button_Click" />  
        </StackPanel>
    </Grid>
</Window>



using System.ComponentModel;
namespace WpfApplication1
{
    public class Model : INotifyPropertyChanged
    {

        private string myField;
        public string MyField
        {
            get { return myField; }
            set
            {
                myField = value;
                Raise("MyField ");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void Raise(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        Model myModel = new Model();


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            myModel.MyField = "has worked";
        }
    }
}

Upvotes: 0

Views: 950

Answers (2)

Nakul Chaudhary
Nakul Chaudhary

Reputation: 26174

Include namespace xmlns:ns="clr-namespace:MyNameSpace"

and use <TextBlock Text="{Binding Source={x:Static ns:MyClass.MyField}}" /> for binding for binding static class.

Whole test code as below: XML file as:

<Window x:Class="WpfApplication2.Test"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ns="clr-namespace:MyNameSpace"
        Title="Test" Height="300" Width="300">
    <DockPanel>
        <TextBlock Text="{Binding Source={x:Static ns:MyClass.MyField}}" Margin="10"/>
    </DockPanel>
</Window>

CODE BEHIND FILE AS

using System.Windows;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for Test.xaml
    /// </summary>
    public partial class Test : Window
    {
        public Test()
        {
            InitializeComponent();

        }
    }
}

namespace MyNameSpace
{
    public static class MyClass
    {
        static MyClass()
        {
            MyField = "Testing";
        }
        public static string MyField {get; set;}
    }
}  

Upvotes: 1

user1672994
user1672994

Reputation: 10849

Suppose your class is not static then you can bind the TextBox to your control with following steps :

  1. Define the namespace:

    xmlns:vms="clr-namespace:Your namespace"
    
  2. Define the DataContext:

    <Window.DataContext><vms:MyClass></vms:MyClass></Window.DataContext>
    
  3. Binding would be as:

    <TextBox Text="{Binding Path="MyField"}/>
    
  4. If you have to update the text from control to source then your VM should implement INotifyPropetyChanged.

    public class MyClass: INotifyPropertyChanged
    {   
    private string myField ;
    public string MyField 
    {
        get { return MyField ; }
        set 
        { 
            MyField = value;
            Raise("MyField ");
        }
    }
    
    public event PropertyChangedEventHandler PropertyChanged;
    
    public void Raise(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    }
    

Upvotes: 2

Related Questions