0

Combobox在一个数据模板中定义了ItemsControl.这个ComboBox,其中Button定义了一个。在Button_Click事件中,Popup应显示 a。这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;
}

问题是,当我单击其中定义的任何控件时myCustomViewPopup会失去焦点并关闭。我怎样才能强制它保持打开状态?

编辑 1:

由于myCustomView有自己的ViewModel,我试图Popup通过将其属性绑定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

4

2 回答 2

1

您可以将 设置PlacementTarget为父级ItemsControl,然后设置VerticalOffset和的HorizontalOffset属性Popup以指定其在屏幕上的确切位置,例如:

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;
}
于 2017-06-19T10:21:57.693 回答
0

您可以像这样将Popup.StaysOpen设置为 true

<Popup StaysOpen="True"/>
于 2017-06-19T08:54:49.240 回答