【问题标题】:How to bind to a list that nested deep in the DataContext object in WPF如何绑定到嵌套在 WPF 中 DataContext 对象深处的列表
【发布时间】:2014-09-04 13:29:40
【问题描述】:

我需要绑定到隐藏在 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; }
} 

I have a list of users, bound to Users property and when a user is selected, I want to display settings defined for the user.设置保存在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 不是绑定的合法路径?有什么想法吗?

【问题讨论】:

    标签: c# wpf xaml data-binding


    【解决方案1】:

    使用Binding Paths 引用嵌套属性与在代码中引用相同属性非常相似。举个例子:

    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 Property 页面。

    【讨论】:

      【解决方案2】:

      您能否尝试使用此绑定代替您的绑定:

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

      【讨论】:

        【解决方案3】:

        基本上当你这样做时:

        UserDataContext.UserSettings = _usersSettingsDal.ToViewModel(settings);
        

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

        RaiseProperyChanged(()=>UserDataContext.UserSettings);
        

        而且绑定也不正确。正如@Greenonion 所说,正确的是:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-12
          • 2013-12-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多