Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Textbox on window does not update programatically, even though I have set two binding. One way does work

$
0
0

xaml

<

Windowx:Class="WpfApplicationCheckBindings.MainWindow"


      

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"


      

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"


      

Title="MainWindow"Height="507.7"Width="564"Loaded="Window_Loaded">


   

<Grid>


       

<TextBoxHorizontalAlignment="Left"Height="23"Margin="337,77,0,0"TextWrapping="Wrap"VerticalAlignment="Top"Width="120"Name="text2"


               

Text="{BindingPath=PersonName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">


        

       

</TextBox>


       

<LabelContent="Label"HorizontalAlignment="Left"Margin="95,77,0,0"VerticalAlignment="Top"/>


       

<ButtonContent="Button"HorizontalAlignment="Left"Margin="122,157,0,0"VerticalAlignment="Top"Width="75"Click="Button_Click"/>


       

<TextBoxName="textBox1"HorizontalAlignment="Left"Height="23"Margin="133,245,0,0"TextWrapping="Wrap"Text="TextBox"VerticalAlignment="Top"Width="120"/>


   

</Grid>

</

Window>

publicpartialclassMainWindow:Window


    {

       

protectedPersonperson = newPerson();

       

SourceClassSourceObject;

       

publicMainWindow()

        {

            InitializeComponent();

            person.PersonName =

"Fred";

           

this.DataContext = person;

            SourceObject =

newSourceClass();

           

Bindingbind =newBinding();

            bind.Source = SourceObject;

            bind.Path =

newPropertyPath("datavalue");

            bind.Mode =

BindingMode.TwoWay;

SourceObject.datavalue = 10;

            textBox1.SetBinding(

TextBox.TextProperty, bind);

        }

       

privatevoidWindow_Loaded(objectsender,RoutedEventArgse)

        {

           

        }

       

privatevoidButton_Click(objectsender,RoutedEventArgse)

        {

            person.PersonName =

"ABC";  //does not work...

        }

    }

 

publicclassPerson:INotifyPropertyChanged


    {

       

privatestringname;

       

publiceventPropertyChangedEventHandlerPropertyChanged;

       

publicPerson()

        { }

       

publicPerson(stringvalue)

        {

           

this.name = value;

        }

       

publicstringPersonName

        {

           

get{returnname; }

           

set


            {

               name =

value;

               

// Call OnPropertyChanged whenever the property is updated


                OnPropertyChanged(

"PersonName");

              

            }

        }

       

protectedvoidOnPropertyChanged(stringname)

        {

           

PropertyChangedEventHandlerhandler = PropertyChanged;

           

if( handler !=null)

            {

                handler(

this.PropertyChanged,newPropertyChangedEventArgs(null));

            }

        }

    }


Howard Davis


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>