WPF(Binding of ObjectDataProvider)

本文介绍了一个使用WPF进行GUI开发的例子,展示了如何通过ObjectDataProvider实现数据绑定,以达到实时更新界面显示效果的目的。同时,还提供了一个简易计算器的实现方式,用于处理输入并返回计算结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

    
}


转载于:https://www.cnblogs.com/wjchang/archive/2013/04/09/3671671.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值