Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 467
Louro Avatar asked Dec 13 '22 01:12

Louro


2 Answers

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}}"
like image 108
dowhilefor Avatar answered Jan 18 '23 21:01

dowhilefor


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}}" 
like image 34
Tilak Avatar answered Jan 18 '23 21:01

Tilak