• WPF(Binding of ObjectDataProvider)


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


  • 相关阅读:
    Unity接入九游SDK学习与踩坑
    Linux系统下实现Tomcat的安装和项目的部署:
    mac查看端口占用情况并结束进程
    BootStrap
    ES6
    JQuery
    CSS 、JavaScript
    HTML
    XML
    数据库连接池&DBUtils
  • 原文地址:https://www.cnblogs.com/wjchang/p/3671671.html
Copyright © 2020-2023  润新知