如何将数据绑定到视图模型中定义的属性的子属性

本文关键字:属性 定义 模型 视图 数据绑定 | 更新日期: 2023-09-27 18:35:24

>我有以下 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));
        }
    }
}

加载主页部分中该部分。UserStatus 有一个属性填满了(我已经调试过,所以它肯定在那里),路由:section.UserStatus.profile.name 显然这反映在向视图模型公开的属性中:UserStatus.profile.name

如何访问 xaml 中的子属性?

如果要访问的属性直接在那里,我还有其他属性可以工作(请参阅组织模板

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

如何将数据绑定到视图模型中定义的属性的子属性

啊,这是因为你不能在列表框中使用单个项目......呵呵:

<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>