<Window
  x:Class="BindingToMethod.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:sys="clr-namespace:System;assembly=mscorlib"
  xmlns:local="clr-namespace:BindingToMethod" 
  Title="Random Numbers Built to Order">

  <Window.Resources>
    <ObjectDataProvider x:Key="nextRandomNumber" ObjectType="{x:Type sys:Random}"  MethodName="Next">
      <ObjectDataProvider.MethodParameters>
        <sys:Int32>0</sys:Int32>
        <sys:Int32>10</sys:Int32>
      </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
    <local:StringToInt32Converter x:Key="numConverter" />
  </Window.Resources>

  <Grid Margin="5" DataContext="{StaticResource nextRandomNumber}">
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition Height="Auto" />
      <RowDefinition Height="Auto" />
      <RowDefinition />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto" />
      <ColumnDefinition />
    </Grid.ColumnDefinitions>

    <Label Grid.Row="0" Grid.Column="0">Min</Label>
    <TextBox Grid.Row="0" Grid.Column="1" Name="minTextBox" ToolTip="{Binding ElementName=minTextBox, Path=(Validation.Errors)[0].ErrorContent}">
      <TextBox.Text>
        <Binding Path="MethodParameters[0]" BindsDirectlyToSource="True" Converter="{StaticResource numConverter}">
          <Binding.ValidationRules>
            <local:IntegerValidator />
          </Binding.ValidationRules>
        </Binding>
      </TextBox.Text>
    </TextBox>

    <Label Grid.Row="1" Grid.Column="0">Max</Label>
    <TextBox Grid.Row="1" Grid.Column="1" Name="maxTextBox" ToolTip="{Binding ElementName=maxTextBox, Path=(Validation.Errors)[0].ErrorContent}">
      <TextBox.Text>
        <Binding Path="MethodParameters[1]" BindsDirectlyToSource="True" Converter="{StaticResource numConverter}">
          <Binding.ValidationRules>
            <local:IntegerValidator />
          </Binding.ValidationRules>
        </Binding>
      </TextBox.Text>
    </TextBox>

    <Label Grid.Row="2" Grid.Column="0">Random #</Label>
    <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding Mode=OneTime}" IsReadOnly="True" />

    <Button Grid.Row="3" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top" Name="nextButton">Next</Button>
  </Grid>
</Window>
