0

I have a ListBox that looks like this:

<ListBox ItemsSource="{StaticResource journal}" DisplayMemberPath="Title" />

When "Title" is a property, e.g:

public string Title { get; set; }

It displays fine, but if I make Title a public field instead, the ListBox only displays an empty string. This unfortunately makes it impossible to work with structs, since they don't support properties.

Are there any workarounds to this?

Hubro
  • 56,214
  • 69
  • 228
  • 381
  • "This makes it impossible to work with structs" is not true. Look for example at [System.Windows.Point](http://msdn.microsoft.com/en-us/library/system.windows.point.aspx). It is a struct with two properties, `X` and `Y` that can be used as source of a binding. – Clemens Jan 20 '13 at 15:09
  • How can you make a struct with properties? I just get the error *"Backing field for automatically implemented property 'Journal.JournalEntry.Title' must be fully assigned before control is returned to the caller. Consider calling the default constructor from a constructor initializer."* – Hubro Jan 20 '13 at 15:13
  • See [here](http://stackoverflow.com/q/420433/1136211). – Clemens Jan 20 '13 at 15:19
  • @Clemens: Yeah I figured it out :) Thanks – Hubro Jan 20 '13 at 15:19

1 Answers1

3

The WPF Binding engine does not support public fields. You'd better create a ViewModel to show these items (with the appropiate properties) if they are structs.

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154