WPF (Binding of ObjectDataProvider)

Source: Internet
Author: User

[Html]
<Window x: Class = "TestOfObjectDataProvider. MainWindow"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "MainWindow" Height = "350" Width = "525">
<StackPanel Background = "LightBlue">
<TextBox x: Name = "testBoxArg1" Margin = "5"/>
<TextBox x: Name = "testBoxArg2" Margin = "5"/>
<TextBox x: Name = "textBoxResult" Margin = "5"/>
</StackPanel>
</Window>

<Window x: Class = "TestOfObjectDataProvider. MainWindow"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "MainWindow" Height = "350" Width = "525">
<StackPanel Background = "LightBlue">
<TextBox x: Name = "testBoxArg1" Margin = "5"/>
<TextBox x: Name = "testBoxArg2" Margin = "5"/>
<TextBox x: Name = "textBoxResult" Margin = "5"/>
</StackPanel>
</Window>
[Csharp]
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;
 
Namespace TestOfObjectDataProvider
{
/// <Summary>
/// Interaction logic for MainWindow. xaml
/// </Summary>
Public partial class MainWindow: Window
{
Public MainWindow ()
{
InitializeComponent ();
This. SetBinding ();
}
 
Private void SetBinding ()
{
ObjectDataProvider odp = new ObjectDataProvider ();
Odp. ObjectInstance = new Calculator ();
Odp. MethodName = "Add ";
Odp. MethodParameters. Add ("");
Odp. MethodParameters. Add ("");
 
Binding bindingToArg1 = new Binding ("MethodParameters [0]")
{
Source = odp,
BindsDirectlyToSource = true,
UpdateSourceTrigger = UpdateSourceTrigger. PropertyChanged
};
 
Binding bindingToArg2 = new Binding ("MethodParameters [1]")
{
Source = odp,
BindsDirectlyToSource = true,
UpdateSourceTrigger = UpdateSourceTrigger. PropertyChanged
};
 
Binding bindingToResult = new Binding (".")
{
Source = odp
};
 
This. testBoxArg1.SetBinding (TextBox. TextProperty, bindingToArg1 );
This. testBoxArg2.SetBinding (TextBox. TextProperty, bindingToArg2 );
This. textBoxResult. SetBinding (TextBox. TextProperty, bindingToResult );
 
}
}
 
Public class Calculator
{
Public string Add (string args1, string args2)
{
Double x = 0;
Double y = 0;
Double z = 0;
If (double. TryParse (args1, out x) & double. TryParse (args2, out y ))
{
Z = x + y;
Return z. ToString ();
}
Return "Input Error! ";
}
}
 

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.