更改Caliburn.micro将用户控件放在窗口上的方式

时间:2013-05-14 22:26:01

标签: wpf caliburn.micro

我有一个使用Caliburn.micro和自定义窗口管理器的应用程序。窗口管理器正在创建我自己的基本窗口,因此我可以自定义整个应用程序的外观。

我想在窗口上添加一些控件,如:

<DockPanel>
    <ContentPresenter Content="{Binding CustomContent}" />
    <StatusBar Height="20" DockPanel.Dock="Bottom" Background="Blue"/>
</DockPanel>

我想Caliburn将来自我的ViewModel的usercontrol放在ContentPresenter中,但是Caliburn正在替换我窗口的整个内容。

我在窗口中这样做了:

using System.Windows;

namespace CaliburnCustomWindow
{
    public partial class WindowBase
    {
        public static readonly DependencyProperty CustomContentProperty = DependencyProperty.Register("CustomContent", typeof (object), typeof (WindowBase));

        public object CustomContent
        {
            get { return GetValue(CustomContentProperty); }
            set { SetValue(CustomContentProperty, value); }
        }

        public WindowBase()
        {
            InitializeComponent();
        }
    }
}

然后修改我的WindowManager来执行此操作:

using System.Windows;
using Caliburn.Micro;

namespace CaliburnCustomWindow
{
    internal class AppWindowManager : WindowManager
    {
        protected override Window EnsureWindow(object model, object view, bool isDialog)
        {
            Window window = view as Window;

            if (window == null)
            {
                if (view.GetType() == typeof (MainView))
                {
                    window = new WindowBase
                    {
                        CustomContent = view,
                        SizeToContent = SizeToContent.Manual
                    };

                    window.Height = 500;
                    window.Width = 500;
                }

                window.SetValue(View.IsGeneratedProperty, true);
            }
            else
            {
                Window owner2 = InferOwnerOf(window);
                if (owner2 != null && isDialog)
                {
                    window.Owner = owner2;
                }
            }
            return window;
        }
    }
}

但它不起作用。绑定到CustomContent依赖项属性似乎不起作用。

可以这样做吗?如果是这样的话?

1 个答案:

答案 0 :(得分:1)

您是否可以使用默认的WindowManager实现,并传入包装器DialogViewModel的新实例(并创建关联的DialogView):

this.WindowManager.ShowDialog(new DialogViewModel(myViewModel));
如果您想简化客户端代码,请在IDialogPresenter或类似的实现中抽象或抽象此代码:

this.DialogPresenter.Show(myViewModel);