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: https://stackoverflow.com/questions/14397728/wpf-mvvm-bind-window-title-to-property#:~:text=The%20title%20is%20bound%20to%20window-viewmodel%20and%20the,because%20it%20inherits%20the%20DataContext%20of%20the%20window.
But I removed the datacontext and it didn't change anything. It showing "MyTitle" like in the preview is the desired behavior.
<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>
This shows in the editor:
But launching the application shows the namespace name followed by some other stuff:
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!
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" });
}