bernieslearnings
bernieslearnings

Reputation: 1664

WPF Rectangle.Fill method responsiveness

Quick performance question for c# / WPF.

I have a rectangle in a grid:

<Rectangle Name="NewPage" Grid.Row="1" Width="42" Height="59.4" Stroke="Black" MouseEnter="Rectangle_MouseEnter_1" MouseLeave="NewPage_MouseLeave_1" />

that I perform a fill on MouseEnter and MouseLeave using:

private void Rectangle_MouseEnter_1(object sender, MouseEventArgs e)
{
   SolidColorBrush blueBrush = new SolidColorBrush();
   blueBrush.Color = Colors.Blue;
   NewPage.Fill = blueBrush;
}

private void NewPage_MouseLeave_1(object sender, MouseEventArgs e)
{
   SolidColorBrush whiteBrush = new SolidColorBrush();
   whiteBrush.Color = Colors.White;
   NewPage.Fill = whiteBrush;
}

The desired goal is to highlight the rectangle as the mouse is moved over it and will allow a click later on to do some more workk.

The issue I'm having is the responsiveness of the fill method. I can move the mouse clear across the rectangle before it is filled. It also doesn't start filling for about 15 seconds after starting the application.

Any tips/guidance on this would be most appreciated.

Thanks!

Upvotes: 0

Views: 831

Answers (1)

sa_ddam213
sa_ddam213

Reputation: 43626

The problem will be caused because the Rectangle is Null when the application starts, this means you wont get a MouseOver event because it goes straight though, It will only turn blue when the Stroke picks up the MouseOver, try setting the Rectangle to White in the xaml first.

<Rectangle Fill="White" Name="NewPage" Grid.Row="1" Width="42" Height="59.4" Stroke="Black" MouseEnter="Rectangle_MouseEnter_1" MouseLeave="NewPage_MouseLeave_1" />

But a better option would be to use a Trigger for this in the Xaml instead. This way you wont need any event handlers in your code behind, and you seperate your UI logic from your actual code base.

    <Rectangle Name="NewPage" Grid.Row="1" Width="42" Height="59.4" Stroke="Black" >
        <Rectangle.Style>
            <Style TargetType="{x:Type Rectangle}">
                <Setter Property="Fill" Value="White" />
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True" >
                        <Setter Property="Fill" Value="Blue" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Rectangle.Style>
    </Rectangle>

Upvotes: 2

Related Questions