Alex F
Alex F

Reputation: 43321

ListBox.DisplayMemberPath doesn't work as expected. How can I debug this?

    <ListBox ItemsSource="{Binding Path=Commands}" DisplayMemberPath="Name"/>

DisplayMemberPath doesn't work, and ListBox shows default ToString result of the Commands collection members. Is it possible to debug this, for example, by printing some information to Output window?

Visual Studio 2010, WPF application project. Binding is successful, and I see all members of the Commandscollection. But display is wrong.

Additional information. If I change Path=Commands to non-existing Path=Commands1, I see error messages in the Output window. But there is no any information about error in DisplayMemberPath.

Upvotes: 3

Views: 1874

Answers (3)

Yaugen Vlasau
Yaugen Vlasau

Reputation: 2218

if you want to use Property "Name" of your "Commands" Item, plese use the following

<ListBox ItemsSource="{Binding Path=Commands}" SelectedItem="{Binding SelectedCommandsItem, Mode=TwoWay}" DisplayMemberPath="Name"/>

Where SelectedCommandsItem is a property of your model that strictly defines a type of the collection items

Upvotes: 0

Chris
Chris

Reputation: 8656

One of the clearer/cleaner ways I've come across for debugging binding errors in WPF is often linked (but using an older, broken link) and can currently be found here: http://www.zagstudio.com/blog/486#.UhyT8fNwbs0

Specifically, the approach that uses a debugging feature introduced in .Net 3.5, using the attached property PresentationTraceSources.TraceLevel and allows you to specify a particular trace level to investigate your binding issues.

Summarising here:

You add the following namespace:

<Window
<!-- Window Code -->
xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"
/>

And in your binding expression, set the attached property. In my example, I'm using a list of Cars objects with a Name property, and have incorrectly listed the DisplayMemberPath as Names:

<ListBox ItemsSource="{Binding Path=Cars, diagnostics:PresentationTraceSources.TraceLevel=High}" DisplayMemberPath="Names" />

This results in the following message in the Output window (occurring multiple times, one for each failed binding):

System.Windows.Data Error: 40 : BindingExpression path error: 'Names' property not found on 'object' ''Car' (HashCode=59988153)'. BindingExpression:Path=Names; DataItem='Car' (HashCode=59988153); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

The whole link is worth a read, but that's the gist of a particular technique I had success with (in case the link dies).

Upvotes: 1

Sheridan
Sheridan

Reputation: 69979

DisplayMemberPath does work... are you sure that you're using it correctly? You can find an example of it on the ItemsControl.DisplayMemberPath Property page at MSDN. For your example code to work, you would need to have a public Name property on the data type in the Commands object.

Failing that, WPF errors generally get output into the Visual Studio Output window. If you do not see any errors there, check that you have the options set correctly:

Go to Tools > Options > Debugging tab > Output Window > WPF Trace Settings

You should have at least one of these options (like Data Binding) set to either Warning, Error, All, Critical or Verbose to receive error information.

Upvotes: 0

Related Questions