110,538
社区成员
发帖
与我相关
我的任务
分享
class Person
{
public string Add(string s, int i)
{
return s + i.ToString();
}
}
<Window x:Class="WPF熊俊.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF熊俊"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="382" Width="401">
<Window.Resources>
<ObjectDataProvider x:Key="odp" ObjectType="{x:Type local:Person}" MethodName="Add">
<ObjectDataProvider.MethodParameters>
<sys:String>熊俊</sys:String>
<sys:Int32>43</sys:Int32>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<StackPanel>
<TextBox x:Name="textBox1" Margin="5" Text="{Binding Source={StaticResource odp}, Path=MethodParameters[0], BindsDirectlyToSource=true, UpdateSourceTrigger=PropertyChanged}" />
<TextBox x:Name="textBox2" Margin="5" Text="{Binding Source={StaticResource odp}, Path=MethodParameters[1], BindsDirectlyToSource=true, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox x:Name="textBox3" Margin="5" Text="{Binding Source={StaticResource odp}, Mode=OneWay}"/>
</StackPanel>
</Window>
public class String2Int : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return System.Convert.ToInt32(value);
}
}
前台改成:
<Window.Resources>
<ObjectDataProvider x:Key="odp" ObjectType="{x:Type local:Person}" MethodName="Add">
<ObjectDataProvider.MethodParameters>
<sys:String>熊俊</sys:String>
<sys:Int32>43</sys:Int32>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<local:String2Int x:Key="string2int"/>
</Window.Resources>
<StackPanel>
<TextBox x:Name="textBox1" Margin="5" Text="{Binding Source={StaticResource odp}, Path=MethodParameters[0], BindsDirectlyToSource=true, UpdateSourceTrigger=PropertyChanged}" />
<TextBox x:Name="textBox2" Margin="5" Text="{Binding Source={StaticResource odp}, Path=MethodParameters[1], BindsDirectlyToSource=true, UpdateSourceTrigger=PropertyChanged,
Converter={StaticResource ResourceKey=string2int}}"/>
<TextBox x:Name="textBox3" Margin="5" Text="{Binding Source={StaticResource odp}, Mode=OneWay}"/>
</StackPanel>