DisplayMemberPath reverts back to Name after setting to something else

181 Views Asked by At

So I am working on an application that uses the MVVM structure to display data to a user and for them to modify it. The issue I am currently having is this:

Upon selecting an entity from a RadGridView, I then add that member the user looked at to an ObservableCollection. I then see that the RadListBox get's populated(meaning I can hover over the item) but nothing is displayed. The Object is a DataContract I am referencing from the services. I've tried a multitude of fixes but to no avail.

At one point I started using Snoop and noticed that no matter what I set the DisplayMemberPath to the value is DisplayMemberPath would always be "Name". And when I was in Snoop if I changed the value DisplayMemberPath to FirstName the name would pop up in my WPF application.

I've been staring at this for awhile and may just need a new set of eyes. Here is the relevant code:

XAML Code:

<Grid MinHeight="70">
  <TextBlock Text="No Members viewed"
             FontWeight="Bold"
             HorizontalAlignment="Center"
             VerticalAlignment="Center"
             Visibility="{Binding TextBlockVisibility, FallbackValue=Visible, Mode=OneWay}">
  </TextBlock>
  <telerik:RadListBox Name="uRecenetlyViewMembersListBox"
          DataContext="{Binding MemberInfo}"
                      SelectionMode="Single"
          DisplayMemberPath="{Binding FirstName}"
                      ItemsSource="{Binding RecentlyViewedMembers}"
                      SelectedItem="{Binding SelectedRecentlyViewedMember,Mode=TwoWay}"
                      Visibility="{Binding WorkingListNoRecordsVisibility, FallbackValue=Visible, Mode=OneWay}" 
                      MouseDoubleClick="uRecenetlyViewMembersListBox_MouseDoubleClick"> 
     <i:Interaction.Triggers>
       <i:EventTrigger EventName="SelectionChanged">
         <i:InvokeCommandAction Command="{Binding RecentlyViewedSelectionChanged}">
         </i:InvokeCommandAction>
       </i:EventTrigger>
     </i:Interaction.Triggers>
  </telerik:RadListBox>
</Grid>

2/11 EDIT: So I removed the DataContext as it was unclear if it even worked as the context is pointing to correct ViewModel. And changed the DisplayMemberPath and removed binding.

<Grid MinHeight="70">
  <TextBlock Text="No Members viewed"
             FontWeight="Bold"
             HorizontalAlignment="Center"
             VerticalAlignment="Center"
             Visibility="{Binding TextBlockVisibility, FallbackValue=Visible, Mode=OneWay}">
  </TextBlock>
  <telerik:RadListBox Name="uRecenetlyViewMembersListBox"
                      SelectionMode="Single"
                      DisplayMemberPath="FirstName"
                      ItemsSource="{Binding RecentlyViewedMembers}"
                      SelectedItem="{Binding SelectedRecentlyViewedMember,Mode=TwoWay}"
                      Visibility="{Binding WorkingListNoRecordsVisibility, FallbackValue=Visible, Mode=OneWay}" 
                      MouseDoubleClick="uRecenetlyViewMembersListBox_MouseDoubleClick"> 
     <i:Interaction.Triggers>
       <i:EventTrigger EventName="SelectionChanged">
         <i:InvokeCommandAction Command="{Binding RecentlyViewedSelectionChanged}">
         </i:InvokeCommandAction>
       </i:EventTrigger>
     </i:Interaction.Triggers>
  </telerik:RadListBox>
</Grid>

And c# code:

private const UInt16 recentlyViewedMemberCapacity = 10;
private ObservableCollection<Member> recentlyViewedMembers;
public ObservableCollection<Member> RecentlyViewedMembers
{
    get { return this.recentlyViewedMembers; }
    set
    {
        if (value != recentlyViewedMembers)
        {
            this.recentlyViewedMembers = value;
            //ugly & inefficient: needs refactoring
            //recentlyViewedMembers = (ObservableCollection<RecentlyViewedMember>)recentlyViewedMembers.OrderByDescending(item => item.DateAdded);
            while (recentlyViewedMembers.Count > recentlyViewedMemberCapacity)
                recentlyViewedMembers.RemoveAt(recentlyViewedMemberCapacity);
            this.RaisePropertyChanged(() => this.RecentlyViewedMembers);
            ValidateWorkingListVisibility();
        }
    }
}

Here is the DataContract returned from the Service, with only necessary pieces:

[DataContract(Name = "Member")]
public class Member : INotifyPropertyChanged
{
    public Member();

    [DataMember(Order = 0)]
    public string FirstName { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;
}
1

There are 1 best solutions below

2
On

Try to set DisplayMemberPath to hardcoded property name instead of binding :

DisplayMemberPath="FirstName"

In standard WPF's ListBox, DisplayMemberPath should be set to a path (means property name) to be displayed, not the value. So in my understanding, even if the binding works, it will set DisplayMemberPath to value of FirstName property instead of the property name.

Hope this works for telerik's RadListBox too.