【问题标题】:Not being able to change Title in a Caliburn.Micro Conductor View using MahApps MetroWindow无法使用 MahApps MetroWindow 在 Caliburn.Micro Conductor 视图中更改标题
【发布时间】:2015-07-21 13:51:16
【问题描述】:

我正在这样做:

<Controls:MetroWindow x:Class="BS.Expert.Client.App.Views.ShellView"
    xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    ShowTitleBar="True"
    Title="My Title">

问题是,这同时是主窗口上定义的主导体,我通过它控制通过其他窗口的导航,因此我无法从 MetroWindow 继承,至少尝试更改 ViewModel 中的标题:

public class ShellViewModel : Conductor<IScreen>.Collection.OneActive, IShell
{
    public ShellViewModel()
    {
        #region mahApps Theme Loading

        var theme = ThemeManager.DetectAppStyle(Application.Current);
        var appTheme = ThemeManager.GetAppTheme(App.Configuration.Theme);
        ThemeManager.ChangeAppStyle(Application.Current, theme.Item2, appTheme);

        #endregion
        //TODO: Changing Title here is not possible ((MetroWindow)this).Title = "No way";
        // Tudo bem na seguinte liña
        LocalizeDictionary.Instance.Culture = new System.Globalization.CultureInfo("pt-BR");
        ShowPageOne();
    }

    public void ShowPageOne()
    {
        ActivateItem(new PageOneViewModel());
    }
}

我应该如何更改标题?

【问题讨论】:

    标签: caliburn.micro mahapps.metro


    【解决方案1】:

    当使用 MVVM 模式时,你不应该尝试像这样直接在视图模型中对视图进行任何设置。而是使用数据绑定来完成此操作。

    所以你会在你的 ShellViewModel 上有一个属性,比如:

    public string MyTitle
    {
        get { return _myTitle; }
        set
        {
            _myTitle = value;
            //Insert your property change code here (not sure of the caliburn micro version)
        }
    }
    

    在你的窗口 xaml 中它会是这样的:

    <Controls:MetroWindow
        Title="{Binding MyTitle}"
        xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        ...
        >
    

    【讨论】:

    • 从 MetroWindow 继承并设置 Title 属性不是您想要做的。即使您确实更改了继承,它也不允许您以这种方式从视图模型更改视图的标题。 ShellView 和 ShellViewModel 只是 MetroWindow 的 2 个不同实例。如果您想设置标题,请实现我描述的内容,只需将视图模型中的 MyTitle 属性设置为您想要的值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    相关资源
    最近更新 更多