I am using a DataTemplate to add Rectangles to an ItemsControl. The rectangles are specified in an ObservableCollection that is assigned as the ItemsSource of the ItemsControl. The rectangle model specifies translations, rotations and scalings.
What I don't understand: why can I bind something to a transform when Transform or TransformGroup are not FrameworkElements and thus don't support inheriting the DataContext?
The XAML-compiler even gives me errors:
(System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Scale; DataItem=null; target element is 'ScaleTransform' (HashCode=35912612); target property is 'ScaleX' (type 'Double'))
but nevertheless the display of the items works fine. This is weird, isn't it.
<ItemsControl.ItemTemplate>
<DataTemplate>
<Rectangle Width="100" Height="100" Fill="Red">
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform
ScaleX="{Binding Scale}"
ScaleY="{Binding Scale}" />
<RotateTransform
Angle="{Binding Angle}" />
<TranslateTransform
X="{Binding X}"
Y="{Binding Y}" />
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
</DataTemplate>
</ItemsControl.ItemTemplate>
Model:
public class RectangleModel
{
public double X {get; private set;}
public double Y {get; private set;}
public double Scale {get; private set;}
public double Angle {get; private set;}
public RectangleModel(double x, double y, double scale, double angle)
{
X = x;
Y = y;
Scale = scale;
Angle = angle;
}
}