0

I am new to Xaml and binding concepts. How to bind the 'CustomerName' property of MainClass with text content of 'TextBox1' in XAML ?

Here is my MainClass,

namespace TextBinding.Module
{
    public class MainClass
    {
        public string CustomerName { get; set; }
    }

}

And my XAML coding is,

<UserControl x:Class="TextBinding.Design.ControlDesigner"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:TestControl="clr-namespace:TextBinding.Module"
         mc:Ignorable="d" d:DesignHeight="1000" d:DesignWidth="1000">

     <TestControl:MainClass x:Key="Test1" />
     <Grid>
        <TextBox x:Name="TextBox1" Height="50" Text="{Binding Test1.CustomerName, Mode=TwoWay}" />
     </Grid>
</UserControl>

This above method is not working at all. Can anyone suggest a better way to bind.? Thanks in advance.

Vignesh
  • 43
  • 1
  • 5
  • see https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx – M.kazem Akhgary Apr 26 '17 at 04:37
  • 1
    _"is not working at all"_ -- Stack Overflow questions are expected to have more precise problem statements than that. That said, the code you posted doesn't look valid at all; it's puzzling why you thought that should work. Please read the documentation and follow along some of the examples in there. If that doesn't fix your misconceptions, look at the various tutorials on the web and even data binding questions here on Stack Overflow, for more working examples that will show you what the right way to do it is. Please read [mcve] and [ask] before posting any more questions. – Peter Duniho Apr 26 '17 at 04:47
  • As far as the above goes, your `MainClass` object should not be a child of the `UserControl`, nor a resource. Since it's a `UserControl`, you want to avoid making it the `DataContext` for the `UserControl` itself, but you can make it the `DataContext` for e.g. the `Grid` in your `UserControl`. Then you can bind to `CustomerName` instead of `Test1.CustomerName` and it should work. – Peter Duniho Apr 26 '17 at 04:47
  • WPF Data Binding is comprehensively explained in the [Data Binding Overview](https://msdn.microsoft.com/en-us/library/ms752347(v=vs.110).aspx) article on MSDN. You should first read that before you start coding. You may also search the web for *MVVM*. – Clemens Apr 26 '17 at 05:04

2 Answers2

1
    <UserControl x:Class="TextBinding.Design.ControlDesigner"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:TestControl="clr-namespace:TextBinding.Module"
             mc:Ignorable="d" d:DesignHeight="1000" d:DesignWidth="1000">
     <UserControl.Resources>
           <TestControl:MainClass x:Key="Test1" />
        </UserControl.Resources>

         <Grid>
            <TextBox x:Name="TextBox1" Height="50" Text="{Binding CustomerName, Mode=TwoWay}" 
DataContext="{DynamicResource Test1}" />
         </Grid>
    </UserControl>

You need to define class object inside usercontrol resources section. Also you need to specify that class object in DataContext property of textbox.

Akshey Bhat
  • 8,227
  • 1
  • 20
  • 20
0

This is not a good idea, You should try using MVVM and have separate view,View Model to do this kind of stuff.

But for your question below is the answer:

public partial class ControlDesigner: UserControl
    {

      public string CustomerName { get; set; }

    public MainWindow()
    {
       // Set Value
       // CustomerName = "Test Name";
        InitializeComponent();
        this.DataContext = this;
    }
}

Here is the sample MVVM approach for the question:

Create a New WPF application and follow the steps:

  1. Have a Mainwindow.xaml, which will contain the user control.

    <Grid>
      <local:ControlDesigner Grid.Row="1" />
    </Grid>
    
  2. Create a ViewModel and set the datacontext of user MainWindow to ViwModel.

            public class MainWindowViewModel        
            {
                private string _customeName;
    
                public string CustomerName
                {
                    get { return _customeName; }
                    set { _customeName = value; }
                }
    
    
            }
    
            public partial class MainWindow : Window
            {
    
    
                public MainWindow()
                {
                    InitializeComponent();
                    this.DataContext = new MainWindowViewModel();
                }
            }
        }
    
  3. Bind the textblock to view model of parent window:

    TextBlock Text="{Binding CustomerName}">

Simsons
  • 12,295
  • 42
  • 153
  • 269
  • 1
    You should never explicitly set the DataContext of a UserControl, neither to itself nor to some view model instance. Doing so effectively prevents that the UserControl inherits the DataContext from its parent control or window, and often breaks bindings to dependency properties of the control. See e.g. here: http://stackoverflow.com/a/28982771/1136211. – Clemens Apr 26 '17 at 06:27
  • @Clemens, Agree, I will keep this in mind. But will keep this answer open , as OP is not aware how things happen between View and View-Model or even for code behind in this case – Simsons Apr 26 '17 at 06:33
  • If you are seriously answering this question, you should perhaps show a proper MVVM approach with a view model instance in the DataContext of the Window where the UserControl is used. – Clemens Apr 26 '17 at 06:34
  • @Clemens, This will bring several things like INotifyPropertyChanged and ICommand (not relevant) , but does the OP need it for now , I have no problem providing MVVM solution , which I am going to do now in Edit – Simsons Apr 26 '17 at 06:36
  • I'd assume that CustomerName never changes (from the model side), so INPC wouldn't probably be relevant too. – Clemens Apr 26 '17 at 06:37
  • Stackoverflow is behaving funny, not letting have code starting with < !!! – Simsons Apr 26 '17 at 07:08