使用DispatcherTimer在某个时间按下某个按钮时打开一个弹出窗口。这工作正常但弹出窗口保持打开,即使StaysOpen属性设置为false。这是代码:
XAML:
<Grid>
<Button x:Name="_button" Content="open" PreviewMouseDown="Button_PreviewMouseDown" PreviewMouseUp="Button_PreviewMouseUp" Width="100" Height="50"/>
<Popup x:Name="_popup" StaysOpen="False" Width="300" Height="300"/>
</Grid>
代码背后:
public partial class MainWindow : Window {
private DispatcherTimer _dispatcherTimer;
public MainWindow() {
InitializeComponent();
}
private void Button_PreviewMouseUp(object sender, MouseButtonEventArgs e) {
_dispatcherTimer.Stop();
}
private void DispatcherTimer_Tick(object sender, EventArgs e) {
_dispatcherTimer.Stop();
_popup.IsOpen = true;
}
private void Button_PreviewMouseDown(object sender, MouseButtonEventArgs e) {
_dispatcherTimer = new DispatcherTimer();
_dispatcherTimer.Tick += new EventHandler(DispatcherTimer_Tick);
_dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 800);
_dispatcherTimer.Start();
}
}
如果我在没有DispatcherTimer的情况下打开弹出窗口,一切都按预期工作。我的问题是:
谢谢!