Home > Enterprise >  WPF window title doesn't change from xaml
WPF window title doesn't change from xaml

Time:01-28

In xaml, I set my title to a value but it doesn't show in the window when I launch my app. Similar to this post: enter image description here

But launching the application shows the namespace name followed by some other stuff:

enter image description here

I am using Caliburn.micro, which I suspect is part of my issue but I cannot figure out how to get the title to update. This is my only window control in the whole project. My other xaml files are UserControls.

The ShellViewModel.cs file is 90% unused code that is a carry over from a tutorial I did. Here is that code:


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());

            
        }
    }
}

Everything works as expected except for this window title. Please let me know if there are other code bits that would be helpful and I will add them. Thanks!

CodePudding user response:

I personally feel caliburn micro uses some bad techniques. I far prefer the community mvvm toolkit.

The work round for this is to explicitly bind the window title.

Title="{Binding WindowTitle}"

And, of course, add a WindowTitle string property to your viewmodel which returns a more suitable title.

You could probably override tostring on your viewmodel.

CodePudding user response:

In Caliburn.Micro, you should set the DisplayName property of the Conductor to change the window title:

public ShellViewModel() //Constructor
{
    DisplayName = "MyTitle";

    People.Add(new PersonModel { FirstName = "Tim", LastName = "Corey" });
    People.Add(new PersonModel { FirstName = "Bill", LastName = "Jones" });
    People.Add(new PersonModel { FirstName = "Sam", LastName = "Yet" });
}
  • Related