所以我正在研究一个使用MVVM结构向用户显示数据并让他们修改数据的应用程序。我目前面临的问题是:
从RadGridView中选择实体后,我将用户查看的那个成员添加到ObservableCollection中。然后我看到RadListBox得到了填充(意味着我可以将鼠标悬停在项目上),但没有显示任何内容。 Object是我从服务引用的DataContract。我尝试了很多修复,但无济于事。
有一次我开始使用Snoop并注意到无论我将DisplayMemberPath设置为什么值,DisplayMemberPath都将始终为“Name”。当我在Snoop中时,如果我将DisplayMemberPath值更改为FirstName,则会在我的WPF应用程序中弹出名称。
我一直盯着这一段时间,可能只需要一双新的眼睛。以下是相关代码:
XAML代码:
<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编辑:所以我删除了DataContext,因为它不清楚是否因为上下文指向正确的ViewModel而工作。并更改了DisplayMemberPath并删除了绑定。
<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>
和c#代码:
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();
}
}
}
这是从服务返回的DataContract,只有必要的部分:
[DataContract(Name = "Member")]
public class Member : INotifyPropertyChanged
{
public Member();
[DataMember(Order = 0)]
public string FirstName { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
答案 0 :(得分:0)
尝试将DisplayMemberPath
设置为硬编码属性名称而不是绑定:
DisplayMemberPath="FirstName"
在标准WPF的ListBox中,DisplayMemberPath
应设置为要显示的路径(表示属性名称),而不是值。因此,根据我的理解,即使绑定有效,它也会将DisplayMemberPath
设置为FirstName
属性的值而不是属性名称。
希望这也适用于telerik的RadListBox。