开发者

WPF TextBox repaints multiple times with mouse hover

I am using Windows Performance suite (which part of Microsoft Windows SDK) to profile my sample application. I am using the "Perforator" with an option "Show dirty-region update overlay", which enables me to see when and where areas are redrawn in an application.

When I hover mouse over a TextBox control then I see that it is redrawn multiple times and the CPU utilization goes up. I tested with a very simple window with just a TextBox control and a button control.

Is this normal for WPF to redraw control on mouse hover? Is there anything that I can do to minimize this?

Here is the windows that I am using

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
    <Grid>
        <StackPanel Orientation="Horizontal">
            <TextBox Height="25" Width="150" >
            </TextBox>
            <Button Margin="10,0,0,0" Height="2开发者_运维技巧5">1211</Button>
        </StackPanel>
    </Grid>


Its not "normal" for WPF to redraw the control on mouse over, because WPF only updates the region that has changed (using retained mode graphics):

One of the biggest benefits in using retained mode graphics is that WPF can efficiently optimize what needs to be redrawn in the application. Even if you have a complex scene with varying levels of opacity, you generally do not need to write special-purpose code to optimize redrawing (see Intelligent Redrawing in the Retained Mode Graphics section).

The problem in this case is that the active Windows theme is applied to the controls. When you move the mouse over the button, you'll see that the button slightly changes to a "light blue-transparent" color (though this depends upon your active Windows theme). At the same time, once you focus the button, another "animative" type of behavior is applied to the button. You can clearly see this with the dirty-region checked.

If you want to change this, you need to define your own theme, overriding the default-theme-behavior. Here's a post to get you started.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜