I am quite new to wpf, so I have been having troubles with some listbox bindings, here's my scenario:
I have an ObservableCollection bound to my listbox. Let's call this collection "Templates"
This is my MessageTemplateClass class:
public class MessageTemplate{
public virtual long Id { get; set; }
public virtual String Name { get; set; }
public virtual MessageTemplateType MessageTempleateType { get; set; } //some enum
public virtual String Topic{ get; set; }
public virtual String Body{ get; set; }
public override string ToString()
{
return Name;
}
}
I have my Listbox bound to this Templates property:
<ListBox Name="lstTemplates" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ItemsSource="{Binding Templates}"
SelectedItem="{Binding Path=CurrentTemplate}"
DisplayMemberPath="Name">
CurrentTemplate is the currently selected template (which users can edit).
There is a button which, when clicked, does this:
CurrentTemplate = new MessageTemplate();
Templates.Add( CurrentTemplate);
Now, I would like the listbox to display these Templates (which have no name assigned) with some default text, such as "Unnamed *" or something.
So here are my two questions:"
1) How do I specify that if the lisbox item's (message template) name is not set, display an "Unnamed *" label / textblock for it?
2) This is kind of off-topic but when I edit a new template, When I assign a name to it, it will dissapear from the listbox (the empty space in the listbox goes away). Any idea on why could this be?
Any help is greatly appreciated
EDIT:
I am looking to do this strictly via xaml. Assume I have no control over my MessageTemplate class