C# 如何使用弹出控件显示视图?
我需要点击一个按钮来显示一个视图。这是事件的代码C# 如何使用弹出控件显示视图?,c#,wpf,popup,C#,Wpf,Popup,我需要点击一个按钮来显示一个视图。这是事件的代码单击 private void Button_Click(object sender, RoutedEventArgs e) { Button s = sender as Button; //some stuff needed to recognise which button was pressed myPopup.PlacementTarget = s; myPopup.Placement = System.Wi
单击
private void Button_Click(object sender, RoutedEventArgs e)
{
Button s = sender as Button;
//some stuff needed to recognise which button was pressed
myPopup.PlacementTarget = s;
myPopup.Placement = System.Windows.Controls.Primitives.PlacementMode.Top;
myPopup.IsOpen = true;
}
这是在xaml中定义的元素
<Popup IsOpen="False" StaysOpen="True" Name="myPopup">
<view:myCustomView />
</Popup>
问题是,当我在myCustomView中定义的任何控件内单击时,弹出窗口会失去焦点并关闭 我怀疑弹出窗口
是在另一个弹出窗口
中定义的,例如组合框的下拉列表
,对吗
您可以尝试在click事件处理程序中以编程方式创建弹出窗口元素:
private void Button_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 = s;
popup.Placement = System.Windows.Controls.Primitives.PlacementMode.Top;
popup.IsOpen = true;
popup.StaysOpen = true;
}
这将允许您在弹出视图中单击,而不会关闭弹出窗口
。确保视图设置了背景
:
popup.Child = new myCustomView() { Background = Brushes.White };
我尝试了你的解决方案,虽然情况确实稍微好了一点,但我仍然有最后的问题。我可以在弹出窗口内单击而不关闭它,但是当我在视图中定义的任何文本框内单击时,弹出窗口将关闭
因此,将PlacementTarget
设置为父ItemsControl
,然后设置弹出窗口的垂直偏移量
和水平偏移量
属性,以指定其在屏幕上的确切位置,例如:
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;
popup.HorizontalOffset = p.X;
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;//myPopup
没有在任何地方定义…你是说pippup
而是?@Fruchtzwerg是的,很抱歉我编辑了名称,但忘记了该名称。添加allowsttransparency=“True”
到弹出窗口
元素。StaysOpen已设置为true…你确定在单击任意位置后弹出窗口关闭吗?@Fruchtzwerg我不知道它是否关闭或是否消失。我肯定再也看不到它了。如果我可以在弹出窗口打开时锁定UI,那就太好了。他可以在XAML中在其他位置之外定义弹出窗口弹出窗口,也可以在他正在做的时候使用它,不是吗?是的,可能,只要它不在另一个弹出窗口中。但是由于他正在处理点击事件,他也可以在按钮实际被点击时创建弹出窗口。这是一种或另一种方式:)很遗憾,我不在工作,无法复制您的建议。我将在周一更新但是,我能说的是,我的弹出窗口不是在另一个弹出窗口中定义的,而是在一个ItemsControl中定义的,更准确地说,应该触发这个弹出窗口的按钮包含在这个ItemControl模板中定义的组合框中。我不知道这是否是同一件事。另外,在EdPlunkett建议之后,在透明的背景下,我会uld注意到:视图显示在所有其他视图之上,但我的注意力仍然集中在基础视图上。我看到了这一点,因为我可以在组合框的下拉框中移动鼠标,突出显示其元素。我尝试了您的解决方案,虽然情况确实稍好,但我仍然存在关闭问题。我可以在Popu内单击p,但当我在视图中定义的任何文本框内单击时,弹出窗口将关闭。
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;
popup.HorizontalOffset = p.X;
popup.IsOpen = true;
popup.StaysOpen = true;
}