Archive

Archive for the ‘Intermediate’ Category

GeometryGroup.Bounds Performance

August 4, 2009 Mihai Leave a comment

GeometryGroup.Bounds Performace

GeometryGroup.Bounds Performace

I recently worked with GeometryGroup and I noticed the performance impact when using GeometryGroup.Bounds.

For simple geometries, everything seams to work smoothly, but when adding, let’s say … 100 000 geometries to the GeometryGroup, accessing its Bounds member takes a looong time … approx. 3 seconds (on my dev machine).

Needless to say, such a low performance makes the method unusable in most scenarios.

As a workaround for this issue, I found that using Rect class and its Union method to add the Bounds of each piece of Geometry works faster.

10 times faster.

Example:

//Execution time > 3 seconds

GeometryGroup group = new GeometryGroup();
Random random = new Random();
for (int i = 0; i < 100000; i++)
{
RectangleGeometry rectGeometry = new RectangleGeometry(new Rect(random.Next(), random.Next(), random.Next(), random.Next()));
group.Children.Add(rectGeometry);
}

Rect groupBounds = group.Bounds;

//Execution time < 0.3 seconds

GeometryGroup group = new GeometryGroup();
Random random = new Random();
Rect rectBounds= Rect.Empty;
for (int i = 0; i < 100000; i++)
{
RectangleGeometry rectGeometry = new RectangleGeometry(new Rect(random.Next(), random.Next(), random.Next(), random.Next()));
group.Children.Add(rectGeometry);
rectBounds.Union(rectGeometry.Bounds);
}

Rect groupBounds = rectBounds;

Where is SetBinding?

June 3, 2009 Mihai Leave a comment

While you’re programmatically creating binding in WPF you’ve probably used objectName.SetBinding quite a lot.

Having these controls

<Slider Grid.Column=”1″ Minimum=”1″ Maximum=”5″  Grid.Row=”1″ Margin=”5″ Name=”slider2″ SmallChange=”1″ IsSnapToTickEnabled=”True” />
<TextBox Height=”23″ Margin=”5″ Grid.Row=”1″ Name=”textBox2″ VerticalAlignment=”Top” />

you can create a binding between the slider2’s Value and the textBox2’s  Text with the following code

Binding sliderTextBoxBinding = new Binding();
sliderTextBoxBinding.Source = slider2;
sliderTextBoxBinding.Path = new PropertyPath(“Value”);
textBox2.SetBinding(TextBox.TextProperty, sliderTextBoxBinding);

Fairly simple ha?

But what happens when the SetBinding method is simple not there?!

Take this for example:

Having the following controls

<Slider Grid.Column=”1″ Minimum=”1″  Maximum=”5″  Grid.Row=”2″ Height=”30″ VerticalAlignment=”Top” Margin=”5″ Name=”slider3″ SmallChange=”1″ />
<Rectangle Grid.Row=”2″ Height=”10″ Margin=”50″ Name=”rectangle1″ Stroke=”Black” VerticalAlignment=”Top” Fill=”Gray” />

let’s say you want to bind the rectangle1’s ScaleY property of a ScaleTransform applied as a RenderTransform to the slider3’s Value

First of all, you can’t directly bind the two together.

You have to create a ScaleTransform and assign it to the rectangle1’s RenderTransform

ScaleTransform transform = new ScaleTransform();
rectangle1.RenderTransform = transform;

When you modify the transform object the changes will automatically propagate to rectangle1 and you will notice the changes.

Now let’s bind this transform to the slider3 Value.

Binding transformBinding = new Binding();
transformBinding.Source = slider3;
transformBinding.Path = new PropertyPath(“Value”);

transform.SetBinding(ScaleTransform.ScaleYProperty, transformBinding);

But wait! transform has no SetBinding method

Well, in this case, use BindingOperations.SetBinding

“BindingOperations.SetBinding creates and associates a new instance of  BindingExpressionBase with the specified binding target property”. You can find more details here.

So our code now becomes

BindingOperations.SetBinding(transform, ScaleTransform.ScaleYProperty, transformBinding);