title: TemplateBinding with Converter - what is wrong?
body: <p>I'm creating a game desk. I wanted to specify field size (one field is a square) as a attached property and with this data set value of ViewPort which would draw 2x2 matrix (and tile mode would do the rest of game desk).</p>
  
  <p>I'm quite at loss what is wrong because the binding doesn't work.</p>
  
  <p>Testing line in XAML for the behaviour I would like to have:</p>
  
  <pre><code>&lt;DrawingBrush Viewport="0,0,100,100" ViewportUnits="Absolute" TileMode="None"&gt;
  </code></pre>
  
  <p>The game desk is based on this sample of DrawingPaint: <a href="http://msdn.microsoft.com/en-us/library/aa970904.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/aa970904.aspx</a> (an image is here)</p>
  
  <p>XAML:</p>
  
  <pre><code>&lt;Window x:Class="Sokoban.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:Sokoban"
      Title="Window1" Height="559" Width="419"&gt;
      &lt;Window.Resources&gt;
          &lt;local:FieldSizeToRectConverter x:Key="fieldSizeConverter" /&gt;
          &lt;Style x:Key="GameDesk" TargetType="{x:Type Rectangle}"&gt;
              &lt;Setter Property="local:GameDeskProperties.FieldSize" Value="50" /&gt;
              &lt;Setter Property="Fill"&gt;
                  &lt;Setter.Value&gt;
                      &lt;!--&lt;DrawingBrush Viewport="0,0,100,100" ViewportUnits="Absolute" TileMode="None"&gt;--&gt;
                      &lt;DrawingBrush Viewport="{TemplateBinding local:GameDeskProperties.FieldSize, Converter={StaticResource fieldSizeConverter}}" ViewportUnits="Absolute" TileMode="None"&gt;
                          &lt;DrawingBrush.Drawing&gt;
                              &lt;DrawingGroup&gt;
                                  &lt;GeometryDrawing Brush="CornflowerBlue"&gt;
                                      &lt;GeometryDrawing.Geometry&gt;
                                          &lt;RectangleGeometry Rect="0,0,100,100" /&gt;
                                      &lt;/GeometryDrawing.Geometry&gt;
                                  &lt;/GeometryDrawing&gt;
  
                                  &lt;GeometryDrawing Brush="Azure"&gt;
                                      &lt;GeometryDrawing.Geometry&gt;
                                          &lt;GeometryGroup&gt;
                                              &lt;RectangleGeometry Rect="0,0,50,50" /&gt;
                                              &lt;RectangleGeometry Rect="50,50,50,50" /&gt;
                                          &lt;/GeometryGroup&gt;
                                      &lt;/GeometryDrawing.Geometry&gt;
                                  &lt;/GeometryDrawing&gt;
                              &lt;/DrawingGroup&gt;
                          &lt;/DrawingBrush.Drawing&gt;
                      &lt;/DrawingBrush&gt;
                 &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
          &lt;/Style&gt;
      &lt;/Window.Resources&gt;
  
      &lt;StackPanel&gt;
          &lt;Rectangle Style="{StaticResource GameDesk}" Width="300" Height="150" /&gt;        
      &lt;/StackPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Converter and property definition:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Windows.Controls;
  using System.Windows;
  using System.Diagnostics;
  using System.Windows.Data;
  
  namespace Sokoban
  {
      public class GameDeskProperties : Panel
      {
  
          public static readonly DependencyProperty FieldSizeProperty;
  
          static GameDeskProperties()
          {
              PropertyChangedCallback fieldSizeChanged =
                  new PropertyChangedCallback(OnFieldSizeChanged);
              PropertyMetadata fieldSizeMetadata =
                  new PropertyMetadata(50, fieldSizeChanged);
  
              FieldSizeProperty = DependencyProperty.RegisterAttached("FieldSize",
                  typeof(int), typeof(GameDeskProperties), fieldSizeMetadata);
          }
  
          public static int GetFieldSize(DependencyObject target)
          {
              return (int)target.GetValue(FieldSizeProperty);
          }
  
          public static void SetFieldSize(DependencyObject target, int value)
          {
              target.SetValue(FieldSizeProperty, value);
          }
  
  
          static void OnFieldSizeChanged(DependencyObject target,
                                DependencyPropertyChangedEventArgs e)
          {
              Debug.WriteLine("FieldSize just changed: " + e.NewValue);
          }
      }
  
      [ValueConversion(/* sourceType */ typeof(int), /* targetType */ typeof(Rect))]
      public class FieldSizeToRectConverter : IValueConverter
      {
          public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              Debug.Assert(targetType == typeof(int));
  
              int fieldSize = int.Parse(value.ToString());
              return new Rect(0, 0, 2 * fieldSize, 2 * fieldSize);
          }
  
          public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              // should not be called in our example
              throw new NotImplementedException();
          }
      }
  }
  </code></pre>