当父控件获得/失去焦点时隐藏/显示子控件
我正在创建一个文本编辑控件,其中包含 RichTextArea 和用于格式化的工具栏。我希望工具栏仅在控件具有焦点时才可见。然而我发现这在 Silverlight 中很难实现,因为 Silverlight 的焦点 API 非常有限。
控件的结构如下:
<UserControl x:Class="MyRichTextBoxControl">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel x:Name="_formattingToolBar" Grid.Row="0" Orientation="Horizontal">
<Button Content="Bold" ... />
<!-- other buttons -->
</StackPanel>
<RichTextBox x:Name="_richTextBox" Grid.Row="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
</Grid>
</UserControl>
初始刺
首先我尝试了显而易见的方法,我覆盖了父 UserControl 上的 OnGotFocus
和 OnLostFocus
并隐藏/显示了 _formattingToolbar
> 适当地。这不起作用,因为如果子控件获得焦点,则 Silverlight 会认为父控件失去焦点。最终结果是尝试单击工具栏导致其消失。
令人讨厌的解决方案
到目前为止,我找到的唯一解决方案是将事件处理程序连接到每个子控件和父 UserControl 上的 GotFocus 和 LostFocus 事件。事件处理程序将调用 FocusManager.GetFocusedElement()
,如果发现返回的元素是我的 UserControl 的子元素,则保持 _formattingToolbar
可见,否则将其折叠。我确信这会起作用,但它非常丑陋。
这个想法也可能是错误的,因为 GotFocus/LostFocus 是异步触发的,而 GetFocusedElement()
是同步确定的。是否存在竞争条件导致我的想法失败?
有人知道更好的解决方案吗?
I am creating a text editing control that contains a RichTextArea and a toolbar for formatting. I want the toolbar to only be visible when the control has focus. However I am finding this difficult to pull off in Silverlight, as Silverlight's focus API is very limited.
The control is structured like this:
<UserControl x:Class="MyRichTextBoxControl">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel x:Name="_formattingToolBar" Grid.Row="0" Orientation="Horizontal">
<Button Content="Bold" ... />
<!-- other buttons -->
</StackPanel>
<RichTextBox x:Name="_richTextBox" Grid.Row="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
</Grid>
</UserControl>
Initial Stab
At first I tried the obvious, I overrode OnGotFocus
and OnLostFocus
on the parent UserControl and hid/showed _formattingToolbar
appropriately. This does not work, because if a child control gains focus, then Silverlight considers the parent control lost focus. The net result is trying to click on the toolbar causes it to disappear.
Nasty Solution
So far the only solution I have found is to hook up event handlers to the GotFocus
and LostFocus
events on every single child control and the parent UserControl. The event handler will call FocusManager.GetFocusedElement()
and if the returned element is found to be a child of my UserControl, then keep _formattingToolbar
visible, otherwise collapse it. I'm sure this will work, but it's pretty ugly.
It's also possible this idea is faulty because GotFocus/LostFocus are fired asynchronously, while GetFocusedElement()
is determined synchronously. Could there be race conditions causing my idea to fail?
Anyone know of a better solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Nitin Midha,你的想法是对的。这让我回到了最初的尝试,稍微改变一下
OnLostFocus
就可以了:Nitin Midha, you had the right idea. That brought my back to my original attempt and a slight altering of
OnLostFocus
does the trick: