Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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
Wpf 中央带有关闭按钮的动态弹出窗口_Wpf_Popup - Fatal编程技术网

Wpf 中央带有关闭按钮的动态弹出窗口

Wpf 中央带有关闭按钮的动态弹出窗口,wpf,popup,Wpf,Popup,我在窗户上有一个按钮。当我点击按钮时,它应该在按钮附近的弹出窗口中显示控件,当弹出窗口打开时,父窗口应该被禁用。当我们创建自定义控件时,我必须在代码隐藏中实现它 我尝试了下面的代码。pop正在打开,但位于左上角,没有关闭按钮 public MainWindow() { InitializeComponent(); Button button = new Button(); button.Width = 130; butt

我在窗户上有一个按钮。当我点击按钮时,它应该在按钮附近的弹出窗口中显示控件,当弹出窗口打开时,父窗口应该被禁用。当我们创建自定义控件时,我必须在代码隐藏中实现它

我尝试了下面的代码。pop正在打开,但位于左上角,没有关闭按钮

 public MainWindow()
    {
        InitializeComponent();

        Button button = new Button();
        button.Width = 130;
        button.Content = "Table";
        button.Click += _buttonClicked;
        MyDock.Children.Add(button);
    }

    private void _buttonClicked(object sender, RoutedEventArgs e)
    {
        this.IsEnabled = false;
        DataGrid simpleTable = new DataGrid();
        simpleTable.ItemsSource = "ss,d,dd,ggg,rr,tt,yy".Split(',').Select(x => new Item() { Value = x }).ToList();
        DataGridTextColumn textColumn = new DataGridTextColumn();
        textColumn.Width = new DataGridLength(60, DataGridLengthUnitType.Pixel);
        simpleTable.Style = (Style)this.Resources["DataGridStyle"];
        textColumn.Binding = new Binding("Value");
        textColumn.ElementStyle = (Style)this.Resources["TextColumn"];
        simpleTable.Columns.Add(textColumn);
        Popup codePopup = new Popup();

        codePopup.Child = simpleTable;
        codePopup.VerticalAlignment = VerticalAlignment.Center;
        codePopup.HorizontalAlignment = HorizontalAlignment.Center;
        codePopup.Placement = System.Windows.Controls.Primitives.PlacementMode.Center;
        codePopup.IsOpen = true;

        this.IsEnabled = true;
    }
}

public class Item
{
    public string Value { get; set; }
}

对于弹出窗口,您需要自行实现关闭按钮。弹出窗口不包括内置的关闭按钮。如果您不想自己实现,也许可以使用其他控件,如messagebox或其他任何控件


对于定位,您应该像这样添加一个PlacementTarget:
codepoppopup.PlacementTarget=MyDock以便弹出窗口知道相关的UIControl。

听起来您需要一个模式对话框窗口。试试这个:

private void _buttonClicked(object sender, RoutedEventArgs e)
{
    DataGrid simpleTable = new DataGrid();
    simpleTable.ItemsSource = "ss,d,dd,ggg,rr,tt,yy".Split(',').Select(x => new Item() { Value = x }).ToList();
    DataGridTextColumn textColumn = new DataGridTextColumn();
    textColumn.Width = new DataGridLength(60, DataGridLengthUnitType.Pixel);
    simpleTable.Style = (Style)this.Resources["DataGridStyle"];
    textColumn.Binding = new Binding("Value");
    textColumn.ElementStyle = (Style)this.Resources["TextColumn"];
    simpleTable.Columns.Add(textColumn);

    Window codePopup = new Window();
    codePopup.SizeToContent = SizeToContent.WidthAndHeight;
    codePopup.WindowStyle = WindowStyle.ToolWindow;
    codePopup.Owner = this;
    codePopup.Content = simpleTable;
    codePopup.VerticalAlignment = VerticalAlignment.Center;
    codePopup.HorizontalAlignment = HorizontalAlignment.Center;
    codePopup.WindowStartupLocation = WindowStartupLocation.CenterOwner;
    codePopup.ShowDialog();
}

在弹出窗口关闭之前,
ShowDialog()
方法不会返回,这实际上意味着在此之前,
main窗口将被禁用。

将xaml发布到弹出窗口的xaml?如何禁用父窗口?您所做的是正确的,但在按钮处理程序的最后一行再次启用它。启用应该在弹出窗口的关闭事件处理程序中完成,而不是在弹出窗口的打开中完成。在这种情况下,代码不会等待您再次关闭弹出窗口。如果你不想自己实现它,你可以使用一个类似Prism的框架和InteractionRequestTriggers:即使禁用父窗口不起作用,你说的不起作用是什么意思?在我的例子中,显示弹出窗口的按钮是不可计时的,因为它被禁用了。你还想做什么?不,我想在弹出窗口打开时使窗口不可访问。是的。但Windows主题不同,因此我无法进行“窗口显示”。您可以重新设置窗口样式,使其外观符合您的要求:。弹出窗口没有任何关闭按钮,也不是模态。