gpt4 book ai didi

c# - WPF-棱镜 : How to close CustomPopupWindow with button click

转载 作者:太空宇宙 更新时间:2023-11-03 12:31:54 25 4
gpt4 key购买 nike

这个在跟我玩,我似乎无法弄明白 - 我需要一些外部帮助,兄弟们!

我想要一个没有标题、最小化、最大化和关闭按钮的弹出窗口,因为我们想自己设置样式并在弹出窗口的屏幕上添加自定义关闭按钮。

所以我通过这些链接找到了现在的位置:

https://msdn.microsoft.com/en-us/library/ff921081(v=pandp.40).aspx https://msdn.microsoft.com/en-us/library/gg405494(v=pandp.40).aspx https://www.codeproject.com/Articles/269364/MVVM-PRISM-Modal-Windows-by-using-Interaction-Requ

但我仍然不知道如何实现这一目标。基本窗口有“确定”和“取消”按钮,但这些是默认按钮,我不希望这样,这就是我采用“自定义 View ”方式的原因。

这是我主窗口的 xaml:

<Window x:Class="Prototype.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:views="clr-namespace:Prototype.Views"
Height="349.146" Width="727.317"
WindowState="Maximized">
<Grid>
<Button Command="{Binding RaiseCustomPopupViewCommand}">Show Popup Window</Button>

<i:Interaction.Triggers>
<prism:InteractionRequestTrigger SourceObject="{Binding CustomPopupViewRequest, Mode=OneWay}">
<prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True">
<prism:PopupWindowAction.WindowStyle>
<Style TargetType="Window">
<Setter Property="ShowInTaskbar" Value="False"/>
<Setter Property="WindowStyle" Value="None"/>
<Setter Property="ResizeMode" Value="NoResize"/>
</Style>
</prism:PopupWindowAction.WindowStyle>
<prism:PopupWindowAction.WindowContent>
<views:CustomPopupView />
</prism:PopupWindowAction.WindowContent>
</prism:PopupWindowAction>
</prism:InteractionRequestTrigger>
</i:Interaction.Triggers>
</Grid>
</Window>

这里是主窗口的代码:

public class MainWindowViewModel : BindableBase
{
public InteractionRequest<INotification> CustomPopupViewRequest { get; private set; }

public MainWindowViewModel()
{
CustomPopupViewRequest = new InteractionRequest<INotification>();
}

public DelegateCommand RaiseCustomPopupViewCommand => new DelegateCommand(RaiseCustomPopupView, CanRaiseCustomPopupView);

public string InteractionResultMessage { get; private set; }

private void RaiseCustomPopupView()
{
InteractionResultMessage = "";
CustomPopupViewRequest.Raise(new Notification { Content = "Message for the CustomPopupView", Title = "Custom Popup" });
}

private bool CanRaiseCustomPopupView()
{
return true;
}
}

InteractionRequestTrigger 的 SourceObject 是一个 UserControl。

这是它的 xaml:

<UserControl x:Class="Prototype.Views.CustomPopupView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
xmlns:local="clr-namespace:Prototype.Views"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
MinWidth="300" MinHeight="100">
<StackPanel Orientation="Vertical" Background="Gray">
<Button DockPanel.Dock="Right"
Content="Close"/>
</StackPanel>
</UserControl>

如您所见,我在 UserControl 中有一个“关闭”按钮。

我尝试使用命令,但即使这样我也无法访问窗口或执行某些操作来关闭窗口。

是否有某种棱镜命令或我不知道的东西?

是否可以通过按钮按我想要的方式关闭窗口?

任何帮助将不胜感激! :)

最佳答案

没关系,我只是设法自己解决了这个问题:)。

我的想法不同,但我真正想要的是 CancelCommand

这就是我们在 UserControl 中实现它的方式,没有其他更改。一切仍然与上述相同,但 CustomPopup 现在在其 ViewModel 中具有以下内容:

public class CustomPopupViewModel : BindableBase, IInteractionRequestAware
{
public CustomPopupViewModel()
{
CancelCommand = new DelegateCommand(CancelInteraction);
}

private CustomPopupSelectionNotification notification;

public INotification Notification
{
get
{
return this.notification;
}
set
{
if (value is CustomPopupSelectionNotification)
{
this.notification = value as CustomPopupSelectionNotification;
this.OnPropertyChanged(() => this.Notification);
}
}
}

public Action FinishInteraction { get; set; }

public System.Windows.Input.ICommand CancelCommand { get; private set; }

public void CancelInteraction()
{
if (notification != null)
{
notification.SelectedItem = null;
notification.Confirmed = false;
}

FinishInteraction();
}
}

您还会注意到我们有一个名为 CustomPopupSelectionNotification 的类。

代码如下:

public class CustomPopupSelectionNotification : Confirmation
{
public CustomPopupSelectionNotification()
{
Items = new List<string>();
SelectedItem = null;
}

public CustomPopupSelectionNotification(IEnumerable<string> items) : this()
{
foreach (string item in items)
{
Items.Add(item);
}
}

public IList<string> Items { get; private set; }

public string SelectedItem { get; set; }
}

简而言之,我只是取消弹出窗口,而不是尝试关闭它。

然后我将命令 CancelCommand 添加到我的 UserControl 上的“关闭”按钮。

关于c# - WPF-棱镜 : How to close CustomPopupWindow with button click,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42433133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com