Manual Update Target : Binding « Windows Presentation Foundation « C# / C Sharp






Manual Update Target

Manual Update Target
  


<Window x:Class="WpfApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:WpfApplication1" 
  Title="ManualUpdateTarget" Height="135" Width="200">
  <Window.Resources>
    <local:Person x:Key="Tom" Name="Tom" Age="11" />
  </Window.Resources>
  <StackPanel DataContext="{StaticResource Tom}">
    <TextBlock Margin="5" VerticalAlignment="Center">Name:</TextBlock>
    <TextBox Margin="5" Name="nameTextBox" Text="{Binding Path=Name}" />
    <TextBlock Margin="5" VerticalAlignment="Center">Age:</TextBlock>
    <TextBox Margin="5" Name="ageTextBox" Text="{Binding Path=Age}" />
    <Button Margin="5" Width="75" Name="birthdayButton">Birthday</Button>
  </StackPanel>
</Window>

//File:Window.xaml.cs

using System;
using System.Collections.Generic;
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.Shapes;
using System.ComponentModel;

namespace WpfApplication1 {
  public class Person {
    string name;
    public string Name {
      get { return this.name; }
      set {
        if( this.name == value ) { return; }
        this.name = value;
      }
    }

    int age;
    public int Age {
      get { return this.age; }
      set {
        if( this.age == value ) { return; }
        this.age = value;
      }
    }

    public Person() { }
    public Person(string name, int age) {
      this.name = name;
      this.age = age;
    }
  }

  public partial class Window1 : System.Windows.Window {
    public Window1() {
      InitializeComponent();
      this.birthdayButton.Click += birthdayButton_Click;
    }

    void birthdayButton_Click(object sender, RoutedEventArgs e) {
      Person person = (Person)this.FindResource("Tom");
      person.Age = person.Age+1;
      BindingOperations.GetBindingExpression(ageTextBox, TextBox.TextProperty).UpdateTarget();

      Console.WriteLine(person.Name);
      Console.WriteLine(person.Age);
    }
  }
}

   
    
  








Related examples in the same category

1.Bind property of one instantiated controlBind property of one instantiated control
2.Bind to Window itselfBind to Window itself
3.Two level path bindingTwo level path binding
4.Bind RelativeSource's AncestorTypeBind RelativeSource's AncestorType
5.Bind RelativeSource's AncestorType's PathBind RelativeSource's AncestorType's Path
6.Bind Stroke Thickness to SliderBind Stroke Thickness to Slider
7.Bind current time to ButtonBind current time to Button
8.Desktop to ControlDesktop to Control
9.Bind Label To ScrollBarBind Label To ScrollBar
10.Bind ScrollBar To LabelBind ScrollBar To Label
11.Binding With Data ContextBinding With Data Context
12.Long Binding PathLong Binding Path
13.Bind ListBox ItemsSource to DayNames property of DateTimeFormatInfoBind ListBox ItemsSource to DayNames property of DateTimeFormatInfo
14.Bind TextBlock Text to SelectedItem property of ListBoxBind TextBlock Text to SelectedItem property of ListBox
15.Bind To TextBox Back and ForthBind To TextBox Back and Forth
16.Binding FontFamily / FontSize value for current ControlBinding FontFamily / FontSize value for current Control
17.Bind to a Collection with the Master-Detail PatternBind to a Collection with the Master-Detail Pattern
18.Bind to IDataErrorInfo
19.Bind to a collectionBind to a collection
20.DataTemplate for bindingDataTemplate for binding
21.Binding Environment InfoBinding Environment Info
22.Bind to an ADO.NETDataSetBind to an ADO.NETDataSet
23.Add a value converter to a binding using XAMLAdd a value converter to a binding using XAML
24.Custom Dialog with data binding
25.One way and two way bindingOne way and two way binding
26.Object BindingObject Binding
27.Without BindingWithout Binding
28.Master Detail BindingMaster Detail Binding
29.Data binding using collections composed of mixed types of data.Data binding using collections composed of mixed types of data.
30.Text Data BindingText Data Binding
31.List BindingList Binding
32.Async bindingAsync binding
33.Null property bindingNull property binding
34.Binding Property with ExceptionBinding Property with Exception
35.Hierarchical Binding for three level nested objectsHierarchical Binding for three level nested objects
36.BindingOperations.GetBindingExpressionBindingOperations.GetBindingExpression
37.Collection View Source BindingCollection View Source Binding
38.Bind to enum typesBind to enum types
39.Bind to a MethodBind to a Method
40.Bind an ItemsControl to the CollectionViewSource, Set its DisplayMemberPath to display the Name propertyBind an ItemsControl to the CollectionViewSource, Set its DisplayMemberPath to display the Name property
41.Bind to the Values of an EnumerationBind to the Values of an Enumeration
42.Binding Dependency Property to TextBlockBinding Dependency Property to TextBlock
43.Bind Your Objects to UI Control with PropertyBind Your Objects to UI Control with Property
44.Implement INotifyPropertyChanged to notify the binding targets when the values of properties change.Implement INotifyPropertyChanged to notify the binding targets when the values of properties change.
45.Bind to an Existing Object InstanceBind to an Existing Object Instance
46.Digital ClockDigital Clock
47.Formatted Digital ClockFormatted Digital Clock
48.Property changed callbackProperty changed callback