我正在尝试使用此代码在处理开始之前强制我的进度视图绘制:
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);
};
}
}
基本行为注入。我必须这样做的原因是因为否则请等待进度消息直到它完成任何必须做的事情之后才会显示。我正在尝试UpdateLayout
和Measure
,但都无效。每个人都告诉我,我在WPF中做错了。什么,我应该在第二个线程或Dispatcher调用中包装我正在做的每个进程?这看起来很难看。