Louro
Louro

Reputation: 1453

WPF window title with databinding and constant string

I have a description´s textbox that I want to add to some constant text in the window title. Like "Description: " + Description. Can I easily do this in the xaml?

My first approach was to create a new property that returns the Description´s value with the constant string. The problem is that I don´t get PropertyChanged event so the window´s title doesnt refresh. I was thinking of creating an event on the Description´s seter but I think its too ugly.

Can you help my out?

Upvotes: 5

Views: 5145

Answers (3)

Tilak
Tilak

Reputation: 30728

You can achieve this very easily using DataBinding

Create a field Description in ViewModel/DataContext, and use StringFormat

Title = "{Binding Path=Description,Mode=OneWay, StringFormat = Description: {0}}" 

Upvotes: 1

Dan Puzey
Dan Puzey

Reputation: 34218

Creating an event on the Description's setter is the standard practise. Use the PropertyChanged event of the INotifyPropertyChanged interface.

Upvotes: 0

dowhilefor
dowhilefor

Reputation: 11051

Checkout StringFormat when using Binding.

Title="{Binding Path=Description, StringFormat=Description: {0}}"

If you have the textbox and you want to use its text, it works the same way. Give the Textbox a name and use ElementName

Title="{Binding ElementName=myText, Path=Text, StringFormat=Description: {0}}"

Upvotes: 13

Related Questions