WPF窗口标题未从xaml更改

hs1rzwqc  于 2023-01-27  发布在  其他
关注(0)|答案(1)|浏览(162)

在xaml中,我将标题设置为一个值,但是当我启动应用程序时,它并没有显示在窗口中。https://stackoverflow.com/questions/14397728/wpf-mvvm-bind-window-title-to-property#:~:text= %20标题%20被%20绑定%20到%20窗口视图模型%20和%20属性,因为它继承了%20%20窗口的%20数据上下文%20。
但我删除了数据上下文,它没有改变任何东西。它显示“我的标题”一样,在预览是理想的行为。

<Window x:Class="RED.Views.ShellView"
        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:model="clr-namespace:RED.ViewModels"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        mc:Ignorable="d"
        Height="700" Width="900" MinWidth="1100" MinHeight="700" Background="Gray" WindowStartupLocation="CenterScreen"
        Title="MyTitle">
<!--d:DataContext="{x:Type model:ShellViewModel}"-->

<!--more markup-->

</Window>

这将在编辑器中显示:

但是启动应用程序时会显示名称空间名称,后面跟一些其他内容:

我正在使用Caliburn.micro,我怀疑这是我的问题的一部分,但我不知道如何让标题更新。这是我在整个项目中唯一的窗口控件。我的其他xaml文件是用户控件。
cs文件中有90%的代码没有使用,这些代码是从我做的教程中继承下来的。

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Caliburn.Micro;
using RED.Models;
using RED.Views;

namespace RED.ViewModels
{
    
    public class ShellViewModel : Conductor<object>
    {

        //private string _lblLogged;
        //private string lblLoggedInAs;

        private string _firstName = "Tim"; // Don't change this
        private string _lastName;
        private BindableCollection<PersonModel> _people = new BindableCollection<PersonModel>();
        private PersonModel _selectedPerson;

        public ShellViewModel() //Constructor
        {
            People.Add(new PersonModel { FirstName = "Tim", LastName = "Corey" });
            People.Add(new PersonModel { FirstName = "Bill", LastName = "Jones" });
            People.Add(new PersonModel { FirstName = "Sam", LastName = "Yet" });
        }

        public string FirstName
        {
            get { return _firstName; }
            set
            {

                _firstName = value;
                NotifyOfPropertyChange(() => FirstName);
                NotifyOfPropertyChange(() => FullName); //Whenever a value of first name is changed, update fullname
            }
        }


        public string LastName
        {
            get { return _lastName; }
            set
            {
                _lastName = value;
                NotifyOfPropertyChange(() => LastName);
                NotifyOfPropertyChange(() => FullName); //Whenever a value of last name is changed, update fullname
            }
        }

       

        public String FullName
        {
            get { return $"{ FirstName } { LastName }"; }
            
        }


        public BindableCollection<PersonModel> People
        {
            get { return _people; }
            set { _people = value; }
        }


        public PersonModel SelectedPerson
        {
            get { return _selectedPerson; }
            set
            {
                _selectedPerson = value;
                NotifyOfPropertyChange(() => SelectedPerson);
            }
        }

        //Return true or true for yes we can clear the text
        public bool CanClearText(string firstName, string lastName)
        {
            //return !String.IsNullOrWhiteSpace(firstName) || !String.IsNullOrWhiteSpace(lastName);
            if (String.IsNullOrWhiteSpace(firstName) && String.IsNullOrWhiteSpace(lastName))
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        //Perameters should start with lowercase, properties should start with uppercase
        public void ClearText(string firstName, string lastName)
        {
            FirstName = "";
            LastName = "";
        }

        public void btn_Phonebook()
        {

            if (Globals.isLoggedIn == true)
            {
                ActivateItem(new PhonebookViewModel());
            }
            else
            {
                MessageBox.Show("Please sign in to use the phonebook.");
            }
            
        }

        public void btn_eRCPS()
        {
            ActivateItem(new WelcomeViewModel());

            
        }
    }
}

除了这个窗口标题之外,一切都按预期工作。请让我知道是否有其他代码位会有帮助,我会添加它们。谢谢!

agxfikkp

agxfikkp1#

我个人觉得caliburn micro使用了一些糟糕的技术。我更喜欢社区mvvm工具包。
解决方法是显式绑定窗口标题。

Title="{Binding WindowTitle}"

当然,还可以向视图模型添加WindowTitle字符串属性,这样可以返回更合适的标题。
你可以在你的视图模型上重写tostring。

相关问题