在WPF 4.0中强制重新渲染

时间:2011-02-04 21:04:14

标签: wpf behavior repaint

我正在尝试使用此代码在处理开始之前强制我的进度视图绘制:

public static class ProgressBehaviors
{
    public static readonly DependencyProperty ForceRepaintProperty =
        DependencyProperty.RegisterAttached(
        "ForceRepaint",
        typeof(bool),
        typeof(ProgressBehaviors),
        new UIPropertyMetadata(false, OnForceRepaintChanged));

    public static bool GetForceRepaint(FrameworkElement treeViewItem)
    {
        return (bool)treeViewItem.GetValue(ForceRepaintProperty);
    }

    public static void SetForceRepaint(FrameworkElement treeViewItem, bool value)
    {
        treeViewItem.SetValue(ForceRepaintProperty, value);
    }

    static void OnForceRepaintChanged(DependencyObject a_object, DependencyPropertyChangedEventArgs e)
    {
        Border item = a_object as Border;
        if (item == null)
            return;

        item.IsVisibleChanged += (s, ev) =>
            {
                item.UpdateLayout();
                Window window = Window.GetWindow(item);
                if (window != null)
                    window.Measure(window.RenderSize);
            };
    }
}

基本行为注入。我必须这样做的原因是因为否则请等待进度消息直到它完成任何必须做的事情之后才会显示。我正在尝试UpdateLayoutMeasure,但都无效。每个人都告诉我,我在WPF中做错了。什么,我应该在第二个线程或Dispatcher调用中包装我正在做的每个进程?这看起来很难看。

1 个答案:

答案 0 :(得分:0)