Frage

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

War es hilfreich?

Lösung

To set a default String if name is empty you can declare your property like this

private string _name = "";
public string Name { 
get
{
    if(string.IsNullOrEmpty(_name))
    {
        return "Unnamed";
    }
    else
    {
        return _name;
    }
}
 set
{
    _name = value;
}
}

Andere Tipps

Try FallBackValue and TargetNullValue on the binding. You can also go with IValueConverter.

Update: Here you can find an example of how to use TargetNullValue: http://msdn.microsoft.com/en-us/library/system.windows.data.bindingbase.targetnullvalue(v=vs.110).aspx

If the value on your binding is null you can display a default text. It will look like this:

<TextBox Text="{Binding SomeProperty, TargetNullValue=your default value}" />

FallBackValue will work in a similar fashion: http://msdn.microsoft.com/en-us/library/system.windows.data.bindingbase.fallbackvalue(v=vs.110).aspx

The last option I mentioned is the IValueConverter: http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top