<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>
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!"; } } }