LoginSignup
0
1

More than 5 years have passed since last update.

Visual Studio / WPF > ObjectDataProvider > 加算するだけ > TextBlockをMethodParameters[]に割り当てる場合、引数はstring型になる?

Last updated at Posted at 2017-06-08
動作環境
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2

http://gushwell.ldblog.jp/archives/52333750.html
を参考にObjectDataProvider を用いて作成クラスの関数を使う例を勉強中

元記事はString型を大文字にする変換をしている。

以下のようにしてみた。

  • 2つの数値(Int16)を入力する
  • 足し算した結果を表示

TextBlock2つにMethodParameter[]を割り当て、それを使うというのはだいたいの考え。
はまったのは、関数の引数はInt16型ではなく、string型になるということ。

MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 _170608_t1137_caldAdd
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class CalcUtil
    {
        public string CalcAdd(Int16 lhs, Int16 rhs)
        {
            // ここには来ない
            return "DBG40:" + lhs.ToString();
        }

        public string CalcAdd(string lhs, string rhs)
        {
            Int16 lval, rval;
            if (Int16.TryParse(lhs, out lval) == false)
            {
                return "";
            }
            if (Int16.TryParse(rhs, out rval) == false)
            {
                return "";
            }

            return "DBG44:" + (lval + rval).ToString();
        }
    }
}
MainWindow.xaml
<Window x:Class="_170608_t1137_caldAdd.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_170608_t1137_caldAdd"
        xmlns:system="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ObjectDataProvider ObjectType="{x:Type local:CalcUtil}"
                      MethodName="CalcAdd" x:Key="myAdd">
            <ObjectDataProvider.MethodParameters>
                <system:Int16>0</system:Int16>
                <system:Int16>0</system:Int16>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <TextBox Height="24" Name="LHS" >
                <TextBox.Text>
                    <Binding Source="{StaticResource myAdd}"
                     Path="MethodParameters[0]"
                     Mode="OneWayToSource"
                     BindsDirectlyToSource="true"
                     UpdateSourceTrigger="PropertyChanged" />
                </TextBox.Text>
            </TextBox>
            <TextBlock Text="+"/>
            <TextBox Height="24" Name="RHS" >
                <TextBox.Text>
                    <Binding Source="{StaticResource myAdd}"
                     Path="MethodParameters[1]"
                     Mode="OneWayToSource"
                     BindsDirectlyToSource="true"
                     UpdateSourceTrigger="PropertyChanged" />
                </TextBox.Text>
            </TextBox>
            <TextBlock Text="="/>
            <TextBlock Height="28" Name="textBlock1" Margin="5"
                   Text="{Binding Source={StaticResource myAdd}}" />
        </StackPanel>
    </Grid>
</Window>

2017-06-08_11h26_10.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1