I'm trying to bind the "Viewbox" property of an ImageBrush to a class property at code-behind.
I have two images, the idea is that while i move the mouse over the top image, the bottom one shows a part of the top image in a Viewbox, relative to the mouse position. The code is just for testing while i'm learning WPF, so i know it could be much better, sorry.
XAML Code:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="950" Width="525">
...
<Rectangle Grid.Row="0" MouseMove="Rectangle_MouseMove">
<Rectangle.Fill>
<ImageBrush Stretch="UniformToFill" ImageSource="descarga.png"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Grid.Row="1">
<Rectangle.Fill>
<ImageBrush Stretch="UniformToFill" ImageSource="descarga.png" ViewboxUnits="Absolute" Viewbox="{Binding Source=ViewboxRect}"/>
</Rectangle.Fill>
</Rectangle>
C# Code behind:
private Rect _rect;
public MainWindow()
{
InitializeComponent();
}
public Rect ViewboxRect
{
get
{
return _rect;
}
set
{
_rect = value;
}
}
private void Rectangle_MouseMove(object sender, MouseEventArgs e)
{
Point p = e.GetPosition(this);
_rect.X = p.X;
_rect.Y = p.Y;
_rect.Width = p.X + 10;
_rect.Height = p.Y + 10;
}
The code above throws the following exception: System.Windows.Data Error: 6 : 'TargetDefaultValueConverter' converter failed to convert value 'ViewboxRect' (type 'String'); fallback value will be used, if available. BindingExpression:Path=; DataItem='String' (HashCode=1651705780); target element is 'ImageBrush' (HashCode=47805141); target property is 'Viewbox' (type 'Rect') FormatException:'System.FormatException: input string was not in a correct format.
I've tried to change"_rect" to a String, but the same error is thrown. Why does the error says "converter failed to convert value 'ViewboxRect' (type 'String')" while that property is a Rect and not a String?
Thank you.