如何将数据绑定到viewmodel中定义的属性的子属性

时间:2013-10-14 21:34:53

标签: 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}"></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>

我有以下 viewmodel

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));
        }
    }
}

LoadHomeSection 中, section.UserStatus 有一个属性已填满(我已经调试了所以它确定在那里),路由: section.UserStatus.profile .name ,显然这会反映在暴露给viewmodel的属性中: UserStatus.profile.name

如何访问xaml中的子属性?

如果要访问的属性是直接的,我还有其他属性可用(参见 OrganisationTemplate

我看过其他帖子,但我无法让他们的版本发挥作用。

1 个答案:

答案 0 :(得分:0)

啊,这是因为你不能在ListBox中使用单个项目... duhhh:

<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>
相关问题