我正在尝试实现以下内容:
- 用户在数据表格中打开上下文菜单。
- 用户选择上下文菜单项后,会弹出一个窗口并显示一些信息。
- 当用户在应用程序的其他位置(而不是在弹出窗口中)点击时,弹出窗口将关闭。
一切都很好,直到我来到关闭弹出窗口的部分。
从其他地方搜索后我知道我需要将Staysopen属性设置为false(已经这样做了),我还读到最好的方法是将IsOpen值绑定到视图模型中的属性,并将其绑定为双向绑定(也已完成)。
另外,我发现如果我添加一个文本框并单击框内,当我单击弹出窗口外部时,它会如预期那样关闭。
另一件我尝试过但没有成功的事情是通过编程方式设置文本框的键盘焦点,以获得所需的“自动关闭”功能。
以下是代码:
XAML -
<Popup Name="PredictionsPopup" Height="200" Width="200" AllowsTransparency="false" StaysOpen="False" IsOpen="{Binding DisplaySummaryPopup, Mode=TwoWay}">
<StackPanel Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}">
<TextBlock Text="here is some stuff" />
<TextBox Name="hiddenBox" Text="moo"/>
</StackPanel>
</Popup>
当菜单项被选中时,Codebehind会设置ViewModel上的属性。
private void CurrentPredicitions_OnClick(object sender, RadRoutedEventArgs e)
{
PredictionsPopup.Placement = PlacementMode.MousePoint;
ViewModel.DisplaySummaryPopup = true;
}
视图模型属性
public bool? DisplaySummaryPopup
{
get
{
return this.displaySummaryPopup;
}
set
{
this.displaySummaryPopup = value;
RaisePropertyChanged(() => this.DisplaySummaryPopup);
}
}
如果您需要更多细节,请告诉我。