How to Set the Title Bar of a Form Window Using XAML

So – I learned a cool new trick today – how to set the title bar of a form window using XAML and so I thought I would share.  Remember the Service Request project that we created awhile back?  Well – In that original version the title bar would say ‘Form Host’ since I hadn’t done anything to set it:


Now, by adding the bit of XAML below the title bar will read “<Work Item ID> : <Work Item Title>” like this:


The title bar will reflect any changes to the title property as the property is changed.

This is the bit of XAML that needed to be added to the form immediately after the opening <UserControl> element and the before the opening <Grid> element:


            <!-- Set Window Title -->
                    <scwpf:PropertyChangedTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=DataContext}"/>
                    <scwpf:RoutedEventTrigger RoutedEvent="FrameworkElement.Loaded"/>
                    <scwpf:PropertyChangedTrigger Binding="{Binding Path=Title}"/>
                    <scwpf:PropertyChangedTrigger Binding="{Binding Path=Id}"/>
                    <scwpf:PropertyMatchCondition Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=DataContext}" Operation="NotEquals">
                    <scwpf:PropertyMatchCondition Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=IsLoaded}">
                <scwpf:ModifyPropertyAction Binding="{Binding RelativeSource={x:Static RelativeSource.Self},
                        <MultiBinding Mode="OneWay">
                                <scwpf:FormatterConverter>{0}: {1}</scwpf:FormatterConverter>
                            <Binding Path="Id" Mode="OneWay" FallbackValue="[New Service Request]"/>
                            <Binding Path="Title" Mode="OneWay" FallbackValue=""/>

The trigger part tells the form to evaluate the business rule on initial load of the form and anytime the title or ID properties change.  The Conditions indicate that this should only be run when the form has a DataContext and that it is completely loaded.  The ModifyPropertyAction specifies the logic to set the title bar property.  Notice how the Path points to view:FormView:WindowTitle.  That’s the trick to set the title bar.  In this case you could put any kind of XAML inside of the .ValueBinding element.  In this case I used a MultiBinding element and a FormatterConverter to put the Title and ID together separated by a colon.

This update has been checked into the CodePlex project if you want to download it and take a look at it: ChangeList 78032