如何防止 Popup 失去焦点?
How to prevent Popup losing focus?
我在 ItemsControl.
的数据模板中定义了一个 Combobox
这个 ComboBox
中定义了一个 Button
。在 Button_Click
事件中,应显示 Popup
。此 Popup
包含自定义 UserControl
,其中定义了一些控件。
这是我解释问题之前的代码:
<ComboBox x:Name="cb" HorizontalAlignment="Center" Grid.Column="2" Width="140" Visibility="{Binding HasCombobox, Converter={StaticResource BoolToVis}}">
<ComboBox.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{Binding Source={StaticResource cvs}}" />
<ComboBoxItem>
<Button Click="Button_Click" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Content="{x:Static prop:Resources.INSERT_BTN}"/>
</ComboBoxItem>
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
这是 Button_Click
事件:
private void Button_Click(object sender, RoutedEventArgs e)
{
Button s = sender as Button;
var popup = new System.Windows.Controls.Primitives.Popup();
popup.AllowsTransparency = true;
popup.Child = new myCustomView();
popup.PlacementTarget = s;
popup.Placement = System.Windows.Controls.Primitives.PlacementMode.Top;
popup.IsOpen = true;
popup.StaysOpen = true;
}
问题是当我单击 myCustomView
中定义的任何控件时,Popup
失去焦点并关闭。我怎样才能强制它保持打开状态?
编辑 1:
因为 myCustomView
有自己的 ViewModel
我试图通过将 IsOpen
属性 绑定到像这样查看模型:
popup.DataContext = myCustomViewModel;
Binding b = new Binding();
b.Source = myCustomViewModel;
b.Path = new PropertyPath("stayOpened");
b.Mode = BindingMode.TwoWay;
b.UpdateSourceTrigger = UpdateSourceTrigger.Default;
BindingOperations.SetBinding(popup, Popup.IsOpenProperty, b);
// BindingOperations.SetBinding(popup, Popup.StaysOpenProperty, b); tried both IsOpened and StaysOpen
但是焦点开关仍然让我很不爽 Popup
。
您可以像这样将 Popup.StaysOpen 设置为 true
<Popup StaysOpen="True"/>
您可以将 PlacementTarget
设置为父级 ItemsControl
,然后设置 Popup
的 VerticalOffset
和 HorizontalOffset
属性以指定其确切位置在屏幕上,例如:
private void btn_Click(object sender, RoutedEventArgs e)
{
Button s = sender as Button;
System.Windows.Controls.Primitives.Popup popup = new System.Windows.Controls.Primitives.Popup();
popup.AllowsTransparency = true;
popup.Child = new myCustomView();
//some stuff needed to recognise which button was pressed
popup.PlacementTarget = ic; //<-- "ic" is the name of the parent ItemsControl
Point p = s.TranslatePoint(new Point(0, 0), ic);
popup.VerticalOffset = p.Y; //adjust this value to fit your requirements
popup.HorizontalOffset = p.X; //adjust this value to fit your requirements
popup.IsOpen = true;
popup.StaysOpen = true;
}
我在 ItemsControl.
的数据模板中定义了一个 Combobox
这个 ComboBox
中定义了一个 Button
。在 Button_Click
事件中,应显示 Popup
。此 Popup
包含自定义 UserControl
,其中定义了一些控件。
这是我解释问题之前的代码:
<ComboBox x:Name="cb" HorizontalAlignment="Center" Grid.Column="2" Width="140" Visibility="{Binding HasCombobox, Converter={StaticResource BoolToVis}}">
<ComboBox.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{Binding Source={StaticResource cvs}}" />
<ComboBoxItem>
<Button Click="Button_Click" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Content="{x:Static prop:Resources.INSERT_BTN}"/>
</ComboBoxItem>
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
这是 Button_Click
事件:
private void Button_Click(object sender, RoutedEventArgs e)
{
Button s = sender as Button;
var popup = new System.Windows.Controls.Primitives.Popup();
popup.AllowsTransparency = true;
popup.Child = new myCustomView();
popup.PlacementTarget = s;
popup.Placement = System.Windows.Controls.Primitives.PlacementMode.Top;
popup.IsOpen = true;
popup.StaysOpen = true;
}
问题是当我单击 myCustomView
中定义的任何控件时,Popup
失去焦点并关闭。我怎样才能强制它保持打开状态?
编辑 1:
因为 myCustomView
有自己的 ViewModel
我试图通过将 IsOpen
属性 绑定到像这样查看模型:
popup.DataContext = myCustomViewModel;
Binding b = new Binding();
b.Source = myCustomViewModel;
b.Path = new PropertyPath("stayOpened");
b.Mode = BindingMode.TwoWay;
b.UpdateSourceTrigger = UpdateSourceTrigger.Default;
BindingOperations.SetBinding(popup, Popup.IsOpenProperty, b);
// BindingOperations.SetBinding(popup, Popup.StaysOpenProperty, b); tried both IsOpened and StaysOpen
但是焦点开关仍然让我很不爽 Popup
。
您可以像这样将 Popup.StaysOpen 设置为 true
<Popup StaysOpen="True"/>
您可以将 PlacementTarget
设置为父级 ItemsControl
,然后设置 Popup
的 VerticalOffset
和 HorizontalOffset
属性以指定其确切位置在屏幕上,例如:
private void btn_Click(object sender, RoutedEventArgs e)
{
Button s = sender as Button;
System.Windows.Controls.Primitives.Popup popup = new System.Windows.Controls.Primitives.Popup();
popup.AllowsTransparency = true;
popup.Child = new myCustomView();
//some stuff needed to recognise which button was pressed
popup.PlacementTarget = ic; //<-- "ic" is the name of the parent ItemsControl
Point p = s.TranslatePoint(new Point(0, 0), ic);
popup.VerticalOffset = p.Y; //adjust this value to fit your requirements
popup.HorizontalOffset = p.X; //adjust this value to fit your requirements
popup.IsOpen = true;
popup.StaysOpen = true;
}