none
Simplest of All Binding C#

    Question

  • I ask a similar question in this forum when working in VB.  It seems a lot of people turn up their noses when you work in VB.  Also the WPF/C# comunity is much larger.  I finally gave up and converted everything I did to C#.  It was really not that difficult of a chore.  The thing I still cannot get is binding.  Following is my attempt at conversion.  Why does it not work?

    <Window x:Class="Binding_Simple_C.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" 
            Height="162" Width="173" 
            ContentRendered="Window_ContentRendered">
        <Grid>
            <TextBox Height="23"
    				 HorizontalAlignment="Left"
    				 Margin="12,12,0,0"
    				 Name="TextBox1"
    				 VerticalAlignment="Top"
    				 Width="126" />
            <Button Content="Button1"
    				Height="60"
    				HorizontalAlignment="Left"
    				Margin="12,41,0,0"
    				Name="Button1"
    				VerticalAlignment="Top"
    				Width="60" 
                    Click="Button1_Click" />
            <Button Content="Button2"
    				Height="60"
    				HorizontalAlignment="Left"
    				Margin="78,41,0,0"
    				Name="Button2"
    				VerticalAlignment="Top"
    				Width="60" 
                    Click="Button2_Click" />
        </Grid>
    </Window>

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.ComponentModel;
    
    namespace Binding_Simple_C
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
            public BindableString bSource = new BindableString();
    
            private void Button1_Click(object sender, RoutedEventArgs e)
            {
                bSource.bString = "bString1";
            }
    
            private void Button2_Click(object sender, RoutedEventArgs e)
            {
                bSource.bString = "bString2";
            }
    
            private void Window_ContentRendered(object sender, EventArgs e)
            {
    
                Binding bind = new Binding("bString");
                bind.Source = bSource;
                bind.Mode = BindingMode.TwoWay;
                TextBox1.SetBinding(TextBox.TextProperty, bind);
            }
        }
    
        public class BindableString : INotifyPropertyChanged
        {
            private string _bString = string.Empty;
            public string bString
            {
                get { return _bString; }
                set { _bString = value; NotifyPropertyChanged("bString"); }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            protected void NotifyPropertyChanged(String info)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(info));
                }
            }
    
        }
    }

    Please note this code has been fixed to protect the innocent.


    • Edited by MichaelBarb Wednesday, February 29, 2012 9:06 PM
    Wednesday, February 29, 2012 5:51 PM

Answers

  • In TextBox1.SetBinding method you have set TextBlock.TextProperty in first parameter. It should be TextBox.TextProperty.

                Binding bind = new Binding("bString");
                bind.Source = bSource;
                bind.Mode = BindingMode.TwoWay;
                TextBox1.SetBinding(TextBox.TextProperty, bind);


    Gaurav Khanna | Microsoft VB.NET MVP


    Wednesday, February 29, 2012 6:37 PM

All replies

  • I can't see anywhere that you set datacontext.

    It's also more usual to set up the binding in xaml rather than code.

    Wednesday, February 29, 2012 6:10 PM
  • Try this:

              Binding bind = new Binding();
                bind.Source = bSource;
                bind.Path = new PropertyPath("bString");
                bind.Mode = BindingMode.TwoWay;
     
                
                BindingOperations.SetBinding(TextBox1, TextBlock.TextProperty, bind);

    I'm assuming that TextBox1 is a TextBlock based on the Dependency Property you are using.  If it's really a textbox then use TextBox.TextProperty
    • Edited by Sean Coder Wednesday, February 29, 2012 6:29 PM
    Wednesday, February 29, 2012 6:28 PM
  • In TextBox1.SetBinding method you have set TextBlock.TextProperty in first parameter. It should be TextBox.TextProperty.

                Binding bind = new Binding("bString");
                bind.Source = bSource;
                bind.Mode = BindingMode.TwoWay;
                TextBox1.SetBinding(TextBox.TextProperty, bind);


    Gaurav Khanna | Microsoft VB.NET MVP


    Wednesday, February 29, 2012 6:37 PM
  • I am not surprised it was a simple typo.  I could not figure out a mistake in syntax.  I guess I stared at it to long.

    Someone complained in a different forum that binding is really fragile. They were correct, no errors or anything for this. C# is so very strongly typed. I would have expected a type mismatch like this to be flagged.  I must be getting complacent.  This is the things I expect in VB.


    Wednesday, February 29, 2012 9:12 PM