MetroWindow.RightWindowCommands动态创建的MetroWindow

时间:2015-01-15 20:55:21

标签: c# wpf caliburn.micro mahapps.metro

如何通过Caliburn.Micro IWindowManager Show方法在动态创建的MetroWindow中绑定/创建MetroWindow.RightWindowCommands?

例如,我创建了一个自定义IWindowManager实现来始终创建MetroWindow而不是默认Window。因此,每当从Caliburn创建一个新窗口时,它将是MetroWindow实例。

我有一个通过IWindowManager动态创建窗口的逻辑:

ChatManager

public class ChatManager : IChatManager
{
    private readonly IChatWindowSettings chatWindowSettings;
    private readonly IWindowManager windowManager;
    private readonly IChatFactory chatFactory;
    private IDictionary<WeakReference, WeakReference> chats;

    public ChatManager(IChatWindowSettings chatWindowSettings, IWindowManager windowManager, IChatFactory chatFactory)
    {
        this.chatWindowSettings = chatWindowSettings;
        this.windowManager = windowManager;
        this.chatFactory = chatFactory;

        chats = new Dictionary<WeakReference, WeakReference>();
    }

    public void OpenFor(ISender sender)
    {
        var settings = chatWindowSettings.Create();
        var viewModel = CreateOrGetViewModel(sender);
        windowManager.ShowWindow(viewModel, null, settings);
    }
    private IChat CreateOrGetViewModel(ISender sender){//code...}

那些窗口是聊天窗口。这非常有效。但是,我想直接在MetroWindow RightCommands中绑定/创建一个按钮。此按钮将绑定到IChat实现(这是一个视图模型):

public class ChatViewModel : Screen, IChat
{
   public void DoSomething(){}
}

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:3)

这里有一些关于你的问题的想法

调用样本

var view = new MainWindow(new ChatViewModel() { ChatName = "Chat name" });
view.Show();

模型样本

public class ChatViewModel
{
    public string ChatName { get; set; }

    private ICommand chatCommand;
    public ICommand ChatCommand
    {
        get
        {
            return chatCommand
                ?? (chatCommand = new SimpleCommand() {
                    CanExecutePredicate = o => true,
                    ExecuteAction = o => MessageBox.Show("Hurray :-D")
                });
        }
    }
}

后面的窗口代码

public partial class MainWindow : MetroWindow
{
    public MainWindow(ChatViewModel chatViewModel)
    {
        this.DataContext = chatViewModel;
        InitializeComponent();
    }
}

窗口xaml

<Controls:MetroWindow x:Class="MahAppsMetroSample.MainWindow"
                      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                      xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
                      Title="MainWindow"
                      GlowBrush="{DynamicResource AccentColorBrush}"
                      Height="350"
                      Width="525">

    <Controls:MetroWindow.RightWindowCommands>
        <Controls:WindowCommands>
            <Button Content="{Binding ChatName}"
                    Command="{Binding ChatCommand}" />
        </Controls:WindowCommands>
    </Controls:MetroWindow.RightWindowCommands>

    <Grid>

        <!-- the content -->

    </Grid>
</Controls:MetroWindow>

简单命令

public class SimpleCommand : ICommand
{
    public Predicate<object> CanExecutePredicate { get; set; }
    public Action<object> ExecuteAction { get; set; }

    public bool CanExecute(object parameter)
    {
        if (CanExecutePredicate != null)
            return CanExecutePredicate(parameter);
        return true; // if there is no can execute default to true
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public void Execute(object parameter)
    {
        if (ExecuteAction != null)
            ExecuteAction(parameter);
    }
}

希望有所帮助