我想用WPF棱镜+ MahApps.Metro创建一个弹出窗口或对话框,以确认选择(确定,取消)。
我创建了自定义交互:
<i:Interaction.Triggers>
<interactionRequest:InteractionRequestTrigger SourceObject="{Binding DeleteConfirmationRequest, Mode=OneWay}">
<interactionRequest:PopupWindowAction>
<interactionRequest:PopupWindowAction.WindowContent>
<confirmation:ConfirmationDialog/>
</interactionRequest:PopupWindowAction.WindowContent>
</interactionRequest:PopupWindowAction>
</interactionRequest:InteractionRequestTrigger>
</i:Interaction.Triggers>但这将创建一个默认的WPF窗口,该窗口不是地铁样式的。如何将其更改为MahApps.Metro窗口?
也许,另一种方式是使用MahApps.Metro对话框,但我不知道如何使用它与棱镜。
有什么想法吗?
发布于 2015-10-05 14:33:44
您必须做两件事,创建新的MetroWindow对话框,然后重写PopupWindowAction来使用它们。这听起来很长,但只需要10分钟:
因此,首先,创建您自己的从MetroWindow继承的确认和通知窗口,就像任何其他MetroWindow一样。您可以从棱镜源复制原始确认窗口和通知窗口,并按照mahapps快速启动的建议更改它们。所以确认窗口应该是这样的:
<Controls:MetroWindow x:Class="MyApp.DefaultPopupWindows.DefaultConfirmationWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
MinWidth="300" MinHeight="150"
Title="{Binding Title}"
BorderBrush="{DynamicResource AccentColorBrush}"
BorderThickness="1">
<Grid x:Name="LayoutRoot" Margin="5">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ContentControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="0" Content="{Binding Content}"/>
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right">
<Button x:Name="OkButton" Content="OK" Width="75" Height="25" HorizontalAlignment="Right" Margin="0,10,0,0" Click="OkButton_Click" />
<Button x:Name="CancelButton" Content="Cancel" Width="75" Height="25" HorizontalAlignment="Right" Margin="20,10,0,0" Click="CancelButton_Click" />
</StackPanel>
</Grid>
</Controls:MetroWindow>以及后面的相关代码:
using MahApps.Metro.Controls;
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;
using System.Windows;
namespace MyApp.DefaultPopupWindows
{
/// <summary>
/// Interaction logic for ConfirmationChildWindow.xaml
/// </summary>
public partial class DefaultConfirmationWindow : MetroWindow
{
/// <summary>
/// Creates a new instance of ConfirmationChildWindow.
/// </summary>
public DefaultConfirmationWindow()
{
InitializeComponent();
}
/// <summary>
/// Sets or gets the <see cref="IConfirmation"/> shown by this window./>
/// </summary>
public IConfirmation Confirmation
{
get
{
return this.DataContext as IConfirmation;
}
set
{
this.DataContext = value;
}
}
private void OkButton_Click(object sender, RoutedEventArgs e)
{
this.Confirmation.Confirmed = true;
this.Close();
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.Confirmation.Confirmed = false;
this.Close();
}
}
}接下来,您将创建您自己的PopupWindowAction,它扩展了棱镜中的一个。在该类中,您重写了GetWindow函数:
protected override Window GetWindow(INotification notification)
{
MetroWindow wrapperWindow;
if (this.WindowContent != null)
{
wrapperWindow = new MetroWindow();
// If the WindowContent does not have its own DataContext, it will inherit this one.
wrapperWindow.DataContext = notification;
wrapperWindow.Title = notification.Title;
this.PrepareContentForWindow(notification, wrapperWindow);
}
else
{
wrapperWindow = this.CreateDefaultWindow(notification);
}
return wrapperWindow;
}您还必须为"CreateDefaultWindow“提供自己的实现,该实现将创建相应窗口的新MetroWindow版本:
protected new MetroWindow CreateDefaultWindow(INotification notification)
{
MetroWindow window = null;
if (notification is IConfirmation)
{
window = new DefaultPopupWindows.DefaultConfirmationWindow() { Confirmation = (IConfirmation)notification };
}
else
{
window = new DefaultPopupWindows.DefaultNotificationWindow() { Notification = notification };
}
return window;
}最后,在您自己的视图/窗口的InteractionRequest中,您可以指定这个新的PopupWindowAction,而不是棱镜版本。
发布于 2015-07-24 00:21:33
我不知道你是否可以在棱镜5.0中完成这个任务,但是使用新的prism 6.0 (Github),您可以使用一个虚拟方法CreateWindow,您可以子类PopupWindowAction并覆盖它,创建CreateWindow窗口。我使用的是折叠代码:
namespace KPP.Vision.Infrastructure.Interactions
{
public class MetroPopupWindowAction:PopupWindowAction
{
protected override Window CreateWindow()
{
return new MetroPopupWindowView();
}
}
}发布于 2016-03-19 13:01:07
我也有同样的问题,通过这些评论,我可以得到一个解决方案。
I使用Prism 6
1)首先重写PopupWindowAction
/// <summary>
/// it creates a new metro window instead of a window
/// </summary>
/// <returns></returns>
protected override Window CreateWindow()
{
return new MetroPopupWindow();
}
/// <summary>
/// Creates a window with the notification type
/// </summary>
/// <param name="notification"></param>
/// <returns></returns>
private new Window CreateDefaultWindow(INotification notification)
{
Window window = null;
if (notification is IConfirmation)
{
window = new MetroConfirmationWindow {Confirmation = (IConfirmation) notification};
}
else
{
window = new MetroNotificationWindow {Notification = notification};
}
return window;
}
/// <summary>
/// Returns the window to display as part of the trigger action.
/// </summary>
/// <param name="notification">The notification to be set as a DataContext in the window.</param>
/// <returns></returns>
protected override Window GetWindow(INotification notification)
{
Window wrapperWindow;
if (WindowContent != null)
{
wrapperWindow = CreateWindow();
if (wrapperWindow == null)
throw new NullReferenceException("CreateWindow cannot return null");
// If the WindowContent does not have its own DataContext, it will inherit this one.
wrapperWindow.DataContext = notification;
wrapperWindow.Title = notification.Title;
PrepareContentForWindow(notification, wrapperWindow);
}
else
{
wrapperWindow = CreateDefaultWindow(notification);
}
// If the user provided a Style for a Window we set it as the window's style.
if (WindowStyle != null)
wrapperWindow.Style = WindowStyle;
return wrapperWindow;
}2)创建MetroWindow、MetroNotificationWindow和MetroConfirmationWindow 基于默认窗口
示例:MetroPopupWindow.xaml
<controls:MetroWindow x:Class="MetroPopupWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
mc:Ignorable="d"
Title="MetroPopupWindow" TitleCaps="False" SizeToContent="WidthAndHeight">
<Grid>
<ContentControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="0"
Content="{Binding Content}" />
</Grid>
</controls:MetroWindow>MetroNotificationWindow.xaml
<controls:MetroWindow x:Class="MetroNotificationWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
Title="Web Studio" TitleCaps="False" SizeToContent="WidthAndHeight">
<Grid x:Name="LayoutRoot" Margin="5">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ContentControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="0"
Content="{Binding Content}" />
<Button x:Name="OKButton" Content="{lex:Loc Ok}" Width="75" Height="25" HorizontalAlignment="Right"
Margin="0,10,0,0" Grid.Row="1" Click="OKButton_Click" IsDefault="True" />
</Grid>
</controls:MetroWindow>3)更改PopupWindowAction对MetroPopupWindowAction的所有引用
示例:
<i:Interaction.Triggers>
<!-- Trigger listening for the "Raised" event on the source object (of type IInteractionRequest) -->
<interactionRequest:InteractionRequestTrigger
SourceObject="{Binding SaveChangesConfirmationRequest, Mode=OneWay}">
<!-- That window will be show as a modal dialog and centered over this window -->
<windowAction:MetroPopupWindowAction IsModal="True" CenterOverAssociatedObject="True" />
</interactionRequest:InteractionRequestTrigger>
</i:Interaction.Triggers>https://stackoverflow.com/questions/30018969
复制相似问题