Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何防止弹出窗口失去焦点?_C#_Wpf_Popup_Lostfocus - Fatal编程技术网

C# 如何防止弹出窗口失去焦点?

C# 如何防止弹出窗口失去焦点?,c#,wpf,popup,lostfocus,C#,Wpf,Popup,Lostfocus,我在项控件的数据模板中定义了一个组合框。此组合框中定义了一个按钮。在按钮上单击事件,应显示一个弹出窗口。此弹出窗口包含自定义用户控件,其中定义了一些控件 在我解释我的问题之前,下面是代码: <ComboBox x:Name="cb" HorizontalAlignment="Center" Grid.Column="2" Width="140" Visibility="{Binding HasCombobox, Converter={StaticResource BoolToVis}}"&

我在
项控件的数据模板中定义了一个
组合框
组合框
中定义了一个
按钮
。在
按钮上单击
事件,应显示一个
弹出窗口
。此
弹出窗口
包含自定义
用户控件
,其中定义了一些控件

在我解释我的问题之前,下面是代码:

<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>
问题是,当我单击
myCustomView
中定义的任何控件时,
弹出窗口将失去焦点并关闭。我怎样才能强迫它保持打开状态

编辑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
但是焦点开关仍然会关闭我的
弹出窗口

您可以这样设置为true

<Popup StaysOpen="True"/>

您可以将
PlacementTarget
设置为父
项控件
,然后设置
弹出窗口
垂直偏移
水平偏移
属性,以指定其在屏幕上的确切位置,例如:

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;
}
private void btn\u单击(对象发送者,路由目标)
{
按钮s=发送器为按钮;
System.Windows.Controls.Primitives.Popup Popup=新建System.Windows.Controls.Primitives.Popup();
popup.allowsttransparency=true;
popup.Child=新建myCustomView();
//需要一些东西来识别按下了哪个按钮

popup.PlacementTarget=ic;//除非它有不同的效果,否则我已经在事件
按钮点击
popup.IsOpen=true;
。它可以工作,因为我可以在不关闭弹出窗口的情况下点击myCustomIView的背景。问题是焦点的变化这个问题和y有什么区别我们的上一个问题?因为现在我有一个关于焦点丢失的具体问题。上一个问题是关于如何在弹出窗口中显示视图。我认为问题的目标已经改变了。@mm8它起作用了。你可以在这里发布该部分它起作用了。为了完整起见,将弹出窗口放在我的按钮上这样做就足够了:
popup.VerticalOffset=p.Y-myCustomView.Height;
popup.horizontalcoffset=p.X-myCustomView.Width;
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;
}