C# 如何将数据绑定到viewmodel中定义的特性的子特性
我的以下xaml不起作用:C# 如何将数据绑定到viewmodel中定义的特性的子特性,c#,xaml,mvvm,windows-phone-8,C#,Xaml,Mvvm,Windows Phone 8,我的以下xaml不起作用: ... <phone:PhoneApplicationPage.Resources> <DataTemplate x:Key="OrganisationTemplate"> <Grid Margin="40,0,0,0"> <StackPanel> <TextBlock Text="{Binding name}"><
...
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="OrganisationTemplate">
<Grid Margin="40,0,0,0">
<StackPanel>
<TextBlock Text="{Binding name}"></TextBlock>
</StackPanel>
</Grid>
</DataTemplate>
<DataTemplate x:Key="UserStatusTemplate">
<Grid Margin="40,0,0,0">
<StackPanel>
<TextBlock Text="{Binding Path=profile.name}"></TextBlock>
</StackPanel>
</Grid>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
...
<!--Panorama item one-->
<phone:PanoramaItem Header="Home">
<ListBox Name="UserStatus" ItemTemplate="{StaticResource UserStatusTemplate}" ItemsSource="{Binding UserStatus}" Margin="0,0,0,96" IsSynchronizedWithCurrentItem="False"/>
</phone:PanoramaItem>
。。。
...
我有以下视图模型:
public class Sections
{
public IEnumerable<Organisation> Organisations { get; set; }
public User User { get; set; }
public UserStatus UserStatus { get; set; }
}
class DashboardViewModel : INotifyPropertyChanged
{
public Sections sections = new Sections();
private OrganisationRepository organisationRepository { get; set; }
private UserRepository userRepository { get; set; }
public DashboardViewModel()
{
LoadOrganisationSection();
LoadHomeSection();
}
...
private async void LoadHomeSection()
{
userRepository = new UserRepository();
sections.UserStatus = await userRepository.GetStatus();
UserStatus = null;
}
#region properties
public UserStatus UserStatus
{
get
{
return sections.UserStatus;
}
set
{
if (sections.UserStatus != value)
{
//LoginCredentials.Username = value;
OnPropertyChanged();
}
}
}
...
#endregion
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler tempEvent = PropertyChanged;
if (tempEvent != null)
{
// property changed
tempEvent(this, new PropertyChangedEventArgs(propertyName));
}
}
}
公共类部分
{
公共IEnumerable组织{get;set;}
公共用户{get;set;}
公共用户状态用户状态{get;set;}
}
类仪表板视图模型:INotifyPropertyChanged
{
公共部分=新部分();
私有组织存储库组织存储库{get;set;}
私有用户存储库用户存储库{get;set;}
公共仪表板视图模型()
{
LoadOrganizationSection();
LoadHomeSection();
}
...
私有异步void LoadHomeSection()
{
userRepository=newuserrepository();
sections.UserStatus=等待userRepository.GetStatus();
UserStatus=null;
}
#区域属性
公共用户状态用户状态
{
得到
{
返回sections.UserStatus;
}
设置
{
if(sections.UserStatus!=值)
{
//LoginCredentials.Username=值;
OnPropertyChanged();
}
}
}
...
#端区
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
PropertyChangedEventHandler tempEvent=PropertyChanged;
if(tempEvent!=null)
{
//财产变更
tempEvent(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
在LoadHomeSection内部,section.UserStatus有一个填充了路由的属性:section.UserStatus.profile.name(我已经调试过了,所以它确实存在),这显然会反映在viewmodel的属性:UserStatus.profile.name中
如何访问xaml中的子属性?
如果要访问的属性直接在那里,我还有其他可以使用的属性(请参见OrganizationTemplate)
我看过其他帖子,但我无法让它们的版本正常工作。啊,这是因为你不能在列表框中使用单个项目。。。嗯:
<phone:PanoramaItem Header="Home">
<TextBlock Name="UserStatus" Text="{Binding UserStatus.profile.name}" />
<!--<ListBox Name="UserStatus" ItemTemplate="{StaticResource UserStatusTemplate}" ItemsSource="{Binding UserStatus}" Margin="0,0,0,96" IsSynchronizedWithCurrentItem="False"/>-->
</phone:PanoramaItem>