Issues changing Bound Main Window Title dynamically.

Oct 18, 2014 at 9:31 AM
Hey guys I'm hoping someone can help me figure out what I'm doing wrong on my Binding.

I'm using a Main windows to display the Ribbon as a UserControl.

as follows:
<Fluent:RibbonWindow xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns:Fluent="clr-namespace:Fluent;assembly=Fluent"                     
                     xmlns:MHManager="clr-namespace:MHManager"
                     x:Name="mainWindow"
                     x:Class="MHManager.MainWindow"                     
                     Title="{Binding DataContext.Title, ElementName=ribbonControl}"
                     Height="768" 
                     Width="1024"
                     WindowState="Normal"
                     ResizeMode="CanResizeWithGrip"
                     Icon="Icons/MHManagerMetroIcon.ico" SaveWindowPosition="True" OverridesDefaultStyle="True"
                     >
    <DockPanel>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <MHManager:RibbonControl x:Name="ribbonControl" Grid.Row="0" Grid.ColumnSpan="2" DockPanel.Dock ="Top" Loaded="ribbonControl_Loaded" />
            <MHManager:NavigationControl x:Name="navigationPane" Grid.Row="1" Grid.Column="0" DockPanel.Dock="Left" Visibility="{Binding DataContext.NavControl_Visibility, ElementName=ribbonControl}"/>
            <MHManager:StatusbarControl x:Name="statusbarControl" Grid.Row="2" Grid.ColumnSpan="2" DockPanel.Dock="Bottom" Visibility="Collapsed" />
        </Grid>
    </DockPanel>
</Fluent:RibbonWindow>
As you can see I am Binding the Title to a public Variable called Title on the RibbonControl.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.Data;
using System.Collections.Specialized;
using Fluent;
using MHManager.Scripts;
using MHManager;

namespace MHManager
{
    /// <summary>
    /// Interaction logic for RibbonControl.xaml
    /// </summary>
    public partial class RibbonControl : UserControl
    {
        public string Title { get; set; }
        public Visibility NavControl_Visibility { get; set; }

        #region Define Variables

        //static public ObservableCollection<Contact> contactdata;
        //static public CURRENTID currentID = new CURRENTID();
        public string regKeyLocation = "Software" + @"\" + ApplicationAttributes.CompanyName + @"\" + ApplicationAttributes.ProductName + @"\" + "Settings";
        static public DataTable dtContacts = new DataTable();
        static public DataTable dtDealerPacks = new DataTable();
        static public DataTable dtServices = new DataTable();
        public string filename;
        public string tempContact;
        public string mruPath;
        public string connectionString;
        public int SavebuttonMode = 0;  // 0 = newContact, 1 = editContact          

        #endregion      

        #region left, right, mid string functions

        public static string left(string param, int length)
        {
            //we start at 0 since we want to get the characters starting from the
            //left and with the specified length and assign it to a variable
            string result = param.Substring(0, length);
            //return the result of the operation
            return result;
        }

        public static string mid(string param, int startIndex, int length)
        {
            //start at the specified index in the string and get the number of
            //characters depending on the length and assign it to a variable
            string result = param.Substring(startIndex, length);
            //return the result of the operation
            return result;
        }

        public static string right(string param, int length)
        {
            //start at the index based on the length of the string minus
            //the specified length and assign it a variable
            string result = param.Substring(param.Length - length, length);
            //return the result of the operation
            return result;
        }

        #endregion
        public RibbonControl()
        {
            Title = string.Format("Manufactured Housing Manager 2014 {0}", typeof(Ribbon).Assembly.GetName().Version);
            NavControl_Visibility = Visibility.Collapsed; 

            InitializeComponent();
            this.DataContext = this;
            this.CommandBindings.Add(new CommandBinding(System.Windows.Input.ApplicationCommands.Open, fileOpenCommand_Execute));
        }      

        #region Update Document Name on Title bar

        /// <summary>
        /// Updates the <see cref="RibbonWindow.DocumentName"/> property.
        /// </summary>
        private void UpdateDocumentName(string name)
        {
            Title = string.Format("Manufactured Housing Manager 2014 {0}", typeof(Ribbon).Assembly.GetName().Version) + " - " + name;
            //ribbon.Title = Title;
        }

        #endregion
    }
}  
I've added

public string Title { get; set; };
Title = string.Format("Manufactured Housing Manager 2014 {0}", typeof(Ribbon).Assembly.GetName().Version);
this.DataContext = this;

This displays fine after the app initially runs......

But when I try to change the contents of Title which the initial Main Windows Title is bound too.
        private void UpdateDocumentName(string name)
        {
            Title = string.Format("Manufactured Housing Manager 2014 {0}", typeof(Ribbon).Assembly.GetName().Version) + " - " + name;
            //ribbon.Title = Title;
        }
it doesn't update the title.

I thought if the property was set to the Title string and I modified it the main Window being bound to that property should update as well but this isn't happening.

Any Ideas?
Developer
Oct 18, 2014 at 9:42 AM
INotifyPropertyChanged or DependencyProperty?!
Marked as answer by batzen on 10/19/2014 at 8:37 AM
Oct 18, 2014 at 10:29 AM
That Worked Thanks a bunch!

I implemented INotifyPropertyChanged.