简体   繁体   English

如何禁用打开WPF弹出窗口

[英]How to disable opening WPF popup

I have a Popup and a ToggleButton . 我有一个Popup和一个ToggleButton I set a binding like this: 我设置了这样的绑定:

<ToggleButton x:Name="myToggle" Content="{Binding MyData.Title}" />
<Popup IsOpen="{Binding IsChecked, ElementName=myToggle}" >
   <TextBlock Text="{Binding MyData.Details}" />
</Popup>

As you see, I bound the toggle button's content to MyData.Title and the popup's content to MyData.Details . 如您所见,我将切换按钮的内容绑定到MyData.Title ,将弹出窗口的内容MyData.DetailsMyData.Details

Now I had the criteria MyData.ShowDetails . 现在我有了条件MyData.ShowDetails If it is true the popup can open and if it is false the popup should not be opened. 如果为true则可以打开弹出窗口;如果为false则不应打开弹出窗口。

How can I set a binding to achieve this? 如何设置绑定以实现此目的?

I tested these bindings on the Popup but no one works: 我在Popup窗口上测试了这些绑定,但没有人起作用:

Visibility="{Binding MyData.ShowDetails, Converter={StaticResource BooleanToVisibilityConverter}}"

IsEnable="{Binding MyData.ShowDetails}"

I found a special answer in my case. 我找到了一个特殊的答案。 Instead of disabling Popup , I should disable the ToggleButton : 我应该禁用ToggleButton而不是禁用Popup

<ToggleButton x:Name="myToggle" Content="{Binding MyData.Title}" 
              IsEnabled="{Binding MyData.ShowDetails}"/>
<Popup IsOpen="{Binding IsChecked, ElementName=myToggle}" >
   <TextBlock Text="{Binding MyData.Details}" />
</Popup>

It works perfect! 完美的作品!

Note: This is not a general answer for Popup . 注意:这不是Popup的一般答案。 Welcome to anyone who has an answer. 欢迎任何有答案的人。

You could put a panel (Grid ) on top of all the content in your window. 您可以在窗口中所有内容的顶部放置一个面板(Grid)。

That needs to have a background set but it can be low opacity if you still want to see the window content. 需要设置背景,但是如果您仍然想查看窗口内容,则可以设置较低的不透明度。

Make that visible only when the popup is shown and collapse otherwise. 仅在显示弹出窗口时使该窗口可见,否则将其折叠。

Make sure you set focus to your popup when it's shown. 确保将焦点设置在显示的弹出窗口上。

.

Bear in mind. 记住。

Popups are separate windows. 弹出窗口是单独的窗口。

They are intended to be shown briefly and have a number of potential drawbacks if you show them for longer periods. 这些内容旨在简要显示,如果长时间显示,则可能会带来许多潜在的缺点。 EG other applications can appear under them and they don't move with their "parent" window/control. EG其他应用程序可能会显示在其下,并且它们不会随其“父”窗口/控件一起移动。

You might find a modal window is easier and suits better, depending on your exact requirements. 您可能会发现模态窗口更容易且更适合您,具体取决于您的要求。

Just instantiate a window and use 只需实例化一个窗口并使用

PopupWindow newWindow = new PopupWindow();
newWindow.ShowDialog();

Where PopupWindow is just any old window styled to look like you want the popup. 其中PopupWindow只是任何样式看起来像您想要弹出窗口的旧窗口。

This will guarantee the user can't somehow interact with any other window in your app. 这将确保用户无法以某种方式与您应用中的任何其他窗口进行交互。

.

Another possibility is to show your "popup" content in a grid which appears on top of everything inside your main window. 另一种可能性是在网格中显示“弹出”内容,该网格显示在主窗口内所有内容的顶部。

That's how editing data works in this: 这就是编辑数据的方式:

https://gallery.technet.microsoft.com/scriptcenter/WPF-Entity-Framework-MVVM-78cdc204 https://gallery.technet.microsoft.com/scriptcenter/WPF-Entity-Framework-MVVM-78cdc204

The plus or minus of that approach is that it's in the one window. 这种方法的优缺点是它在一个窗口中。 --- Brian Kress --- - 布赖恩·克雷斯 -

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM