【发布时间】:2011-10-11 23:40:46
【问题描述】:
在以下 XAML 中,editPanel 始终可见。仅当通过按 F5 键启动长时间操作时,overlayGrid 才可见。编辑面板变灰的视觉效果会发生漫长的过程。
<Window.InputBindings>
<KeyBinding Key="F5" Command="{Binding Path=RefreshCommand}"/>
</Window.InputBindings>
<Grid>
<StackPanel Name="editPanel">
<TextBox>set focus here to see the problem.</TextBox>
<CheckBox>set focus here to remove the problem.</CheckBox>
<TextBlock Text="{Binding Path=Worker.Message}"/>
</StackPanel>
<Grid Name="overlayGrid"
Visibility="{Binding Path=Worker.Visibility}"
Background="Gray" Opacity="0.5">
<TextBox Text="{Binding Path=Worker.Message}" FontWeight="Bold"
HorizontalAlignment="Center" VerticalAlignment="Center"
/>
</Grid>
</Grid>
overlayGrid 完全按预期显示,除非 TextBox 具有焦点。如果 TextBox 具有焦点,则在您看到 overlayGrid 快速闪烁之前进行长时间操作。就好像代码是:做长操作,显示overlayGrid,折叠overlayGrid。
执行长操作并改变overlayGrid可见性的ViewModel代码如下:
Sub Refresh()
Me.Worker.Message = String.Format("Refresh started at {0}..",
Date.Now.ToString("hh:mm:ss.fff")
)
Me.Worker.Visibility = Visibility.Visible
' Give the UI a chance to update itself.
System.Windows.Forms.Application.DoEvents()
Console.WriteLine("Debug: " + Me.Worker.Message)
' Fake the long operation.
System.Threading.Thread.Sleep(1000)
Me.Worker.Message = String.Format("Refresh completed at {0}.",
Date.Now.ToString("hh:mm:ss.fff")
)
Me.Worker.Visibility = Visibility.Collapsed
Console.WriteLine("Debug: " + Me.Worker.Message)
End Sub
为什么当 TextBox 有焦点时,overlayGrid 的实际可见性会延迟?我该如何解决这个问题?
【问题讨论】:
标签: wpf vb.net grid visibility