繁体   English   中英

如何绑定到WPF中DataContext对象深层嵌套的列表

[英]How to bind to a list that nested deep in the DataContext object in WPF

我需要绑定到隐藏在DataContext对象内部深处的对象列表,它似乎对我不起作用。 这是我的DataContext对象:

public class UserDataContext
{
    public ObservableCollection<UserViewModel> Users { get; set; }
    public UsersSettingsViewModel UserSettings { get; set; }
}


public class UsersSettingsViewModel
{
    public int Id { get; set; }
    public ObservableCollection<Subscriptions> Subscriptions { get; set; }
} 

我有一个绑定到Users属性的用户列表,当选择了一个用户时,我想显示为该用户定义的设置。 设置保存在UsersSettingsViewModel类中,此刻它只是用户订阅的列表。 我在一开始将DataContext设置为UserDataContext的实例。 然后,我在SelectionChanged事件处理程序中为所选用户动态加载UserSettingsViewModel对象:

private void OnUserSelectionChanged(object sender, SelectionChangedEventArgs e)
{
   if (lwUsers.SelectedItem == null)
       return;

   var selectedUser = (UserViewModel)lwUsers.SelectedItem;
   var settings = _usersSettingsDal.Load(selectedUser.Login) ?? _usersSettingsDal.Create(selectedUser.Login);
   UserDataContext.UserSettings = _usersSettingsDal.ToViewModel(settings);
}

尽管从XAML绑定到Users属性没有任何问题,但是由于某种原因,我根本无法显示Subscriptions属性。 它只是什么都不做:

<GroupBox Header="Subscriptions" Name="gbSubscriptions">
                            <StackPanel Margin="10,10,10,10">
                                <ListView ItemsSource="{Binding UserDataContext.Subscriptions}">
                                    <ListBox.ItemTemplate>
                                        <DataTemplate>
                                            <StackPanel>
                                                <TextBlock Text="{Binding Path=Name, diag:PresentationTraceSources.TraceLevel=High}"   />
                                                <CheckBox IsChecked="{Binding Path=Enabled, diag:PresentationTraceSources.TraceLevel=High}" />
                                            </StackPanel>
                                        </DataTemplate>
                                    </ListBox.ItemTemplate>
                                </ListView>
                            </StackPanel>
                        </GroupBox>

我是否需要以某种方式刷新DataContext? 也许UserDataContext.Subscriptions不是Binding的合法路径? 有什么想法吗?

使用Binding Path引用嵌套属性与在代码中引用相同属性非常相似。 请看以下示例:

int age = SomeParent.SomeChild.Age;  

"{Binding Path=SomeParent.SomeChild.Age}"


SomeObject object = SomeCollection[0].SomeChild.SomeObject;

"{Binding Path=SomeCollection[0].SomeChild.SomeObject}"


double value = SomeClass.SomeObjectProperty.SomeCollection[0].SomeProperty;

"{Binding Path=SomeClass.SomeObjectProperty.SomeCollection[0].SomeProperty}"

有关Binding Path语法的更详细列表,请查看MSDN上的Binding.Path属性页。

您能否尝试使用此绑定而不是您的绑定:

<ListView ItemsSource="{Binding Path=UserDataContext.UserSettings.Subscriptions}">

基本上,当您这样做时:

UserDataContext.UserSettings = _usersSettingsDal.ToViewModel(settings);

您应该像@toadflakz所说的那样引发一个propertychange事件,例如:

RaiseProperyChanged(()=>UserDataContext.UserSettings);

并且绑定也不正确。 正确的是@Greenonion说的:

<ListView ItemsSource="{Binding Path=UserDataContext.UserSettings.Subscriptions}">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM