0

I'm following the answers given in this thread here: Access codebehind variable in XAML

Here's what I'm trying to do:

<Rectangle Name="MyRect" Fill="AliceBlue" MouseDown="Rectangle_MouseDown">
    <Rectangle.RenderTransform>
        <TransformGroup>
            <RotateTransform Angle="0" CenterX="300" CenterY="150"/>
            <TranslateTransform X="{DynamicResource TransX}" Y="0"/>
        </TransformGroup>
    </Rectangle.RenderTransform>
</Rectangle>

Then, I have a variable in my code behind which gets changed. It's called TransX and I add it as a resource:

public double TransX = 0;

public SvgPreview()
{
    InitializeComponent();
    SvgPreview1.Resources.Add("TransX", TransX);
}

The rectangle does get transformed properly from the start, however the transforms are not re-rendered to reflect changes in the TransX variable. What should I do?

Also, I have to do this exact same thing for several other values.

Community
  • 1
  • 1
Adam S
  • 8,945
  • 17
  • 67
  • 103

1 Answers1

1

I think you should make SvgPreview implement INotifyPropertyChanged, set it as the DataContext of some parent of your Rectangle, and change TransX into a property and access it via DataBinding.

Edit: Like this:

<Rectangle Name="MyRect" Fill="AliceBlue" MouseDown="Rectangle_MouseDown"> 
    <Rectangle.RenderTransform> 
        <TransformGroup> 
            <RotateTransform Angle="0" CenterX="300" CenterY="150"/> 
            <TranslateTransform X="{Binding TransX}" Y="0"/> 
        </TransformGroup> 
    </Rectangle.RenderTransform> 
</Rectangle> 


class SvgPreview : INotifyPropertyChanged
// ......
private double transX;
public double TransX
{
    get { return transX; }
    set 
    {
        if(transX != value)
        {
            transX = value;
            OnNotifyPropertyChanged("TransX"); 
        } 
    }
}                

public SvgPreview() 
{ 
    InitializeComponent(); 
    TransX = 0;
}

and in the CodeBehehind of the Windows/Page/Control that contains your Rectangle;

// ...
InitializeCompenent();
this.DataContext = new SvgPreview();
// ...
Jens
  • 25,229
  • 9
  • 75
  • 117
  • I'm quite confused. I don't have any such method called "OnNotifyPropertyChanged" – Adam S Nov 22 '10 at 07:48
  • @Adam: Sorry, it looks like this: `public void OnPropertyChanged(String propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); }` – Jens Nov 22 '10 at 07:49
  • @Adam S: Implement the INotifyPropertyChanged interface – Fredrik Hedblad Nov 22 '10 at 07:50
  • Sorry guys, I am sure your solutions are correct, but I am very new and I don't know what it means to "Implement the INotifyPropertyChanged interface" – Adam S Nov 22 '10 at 08:31
  • In your class SvgPreview, declare it like `class SvgPreview : INotiyPropertyChanged`. Once you do this, Visual Studio will offer a quick tooltip on the string INotifyPropertyChanged. Clicking it will offer the option "Implement interface INotifyPropertyChanged". Accept that option, add the method in my comment above, and you have implemented it. Also, you may try a google search for "DataBinding WPF" to read up on what we are talking about =) – Jens Nov 22 '10 at 08:47