0

我有以下不工作的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>

我有以下视图模型

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显然这会反映在暴露给视图模型的属性中:UserStatus.profile.name

如何访问我的 xaml 中的子属性?

如果要访问的属性直接在那里,我还有其他可用的属性(请参阅OrganisationTemplate

我看过其他帖子,但我无法让他们的版本正常工作。

4

1 回答 1

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>
于 2013-10-14T21:39:08.163 回答