除了已有的弹出项目外,是否可以将菜单弹出按钮添加到UWP中的RichEditBox。但我发现richeditbox中没有弹出属性。那么可以添加一个吗?如果是,请提供添加menuFlyout的步骤。提前谢谢!
答案 0 :(得分:2)
当然可能。
<RichEditBox GotFocus="RichEditBox_GotFocus">
<FlyoutBase.AttachedFlyout>
<Flyout>
<Button Content="test"/>
</Flyout>
</FlyoutBase.AttachedFlyout>
</RichEditBox>
private void RichEditBox_GotFocus(object sender, RoutedEventArgs e)
{
FlyoutBase.ShowAttachedFlyout((sender as RichEditBox));
}
<强>更新强>
我尝试在没有自定义弹出窗口的情况下实现您的要求。
<强>观察强>
1 RightTapped
事件不会触发Textbox
。不知道为什么。那里
在ScrollViewer
的{{1}}中ControlTemplate
(可能就是这样)
RightTapped事件未在文本框中触发的原因)所以我补充说
Scrollviewer的RightTapped事件。
2
TextBox
在上面的代码 private async void ContentElement_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
FlyoutBase.ShowAttachedFlyout(textbox);
await Task.Delay(1000);
FlyoutPresenter canvas = testbutton.FindParent<FlyoutPresenter>();
var popup = canvas.Parent as Popup;
double x = e.GetPosition(e.OriginalSource as UIElement).X;
Debug.WriteLine(x);
popup.IsOpen = false;
popup.SetValue(Canvas.LeftProperty, e.GetPosition(e.OriginalSource as UIElement).X);
popup.IsOpen = true;
}
<Style x:Key="RichEditBoxStyle1" TargetType="RichEditBox">
...
<ScrollViewer x:Name="ContentElement" IsRightTapEnabled="True" RightTapped="ContentElement_RightTapped" AutomationProperties.AccessibilityView="Raw" HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}" HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" IsTabStop="False" IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}" IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}" IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}" Margin="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" Grid.Row="1" VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}" VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}" ZoomMode="Disabled"/>
...
</Style>
<RichEditBox x:Name="textbox" Width="400" HorizontalAlignment="Left" Grid.Row="3" RightTapped="RichEditBox_RightTapped" IsRightTapEnabled="True" GotFocus="RichEditBox_GotFocus" Style="{StaticResource RichEditBoxStyle1}">
<FlyoutBase.AttachedFlyout>
<Flyout >
<Button Content="test" x:Name="testbutton" Click="Button_Click"/>
</Flyout>
</FlyoutBase.AttachedFlyout>
</RichEditBox>
中是ScrollViewer的RightTapped事件。添加此项您必须编辑TextBox样式。
我正在使用VisualTreeHelper基本上是flyout conatins弹出窗口。并设置PopUp的位置(我从事件中获得)。但不知何故,PopUp还没有确定到正确的位置。
所以第二个选择是去自定义弹出窗口。请参阅此Link了解如何实施一个。你可以从那里获取代码 这是修改后的代码
ContentElement_RightTapped
更新2
您无法添加到TextBox的现有Contextmenu中。要在此处修改上下文菜单,请sample