2014-04-29 137 views
1

我有一个ObservableCollection类(TopLevel),其中包含一个名称属性和另一个ObservableCollection类(BottomLevel),只有一个名称属性的列表。最高列表上的绑定起作用,但是当我试图绑定到BottomList上的属性时,我什么也得不到。我在这里错过了什么?如何绑定到集合集合下的对象的属性?

XAML:

<Window x:Class="WpfApplication7.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid x:Name="myGrid" DataContext="topList"> 
    <Border BorderBrush="AliceBlue" Grid.Column="0" BorderThickness="5"> 
     <ItemsControl x:Name="ic1"> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <StackPanel> 
         <Label Content="{Binding Path=TopName}"/> 
         <Border BorderBrush="AntiqueWhite" Grid.Column="1" BorderThickness="5"> 
          <Button Content="{Binding Path=BottomList.BottomName}"/> 
         </Border> 
        </StackPanel> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate>     
     </ItemsControl>    
    </Border> 
</Grid> 

后面的代码:

public partial class MainWindow : Window 
{ 
    public ObservableCollection<TopLevel> topList; 
    public MainWindow() 
    { 
     InitializeComponent(); 
     topList = new ObservableCollection<TopLevel>(); 
     topList.Add(new TopLevel("T" + (1 + topList.Count).ToString())); 
     topList[0].AddBottom(); 
     topList.Add(new TopLevel("T" + (1 + topList.Count).ToString())); 
     topList[1].AddBottom(); 
     ic1.ItemsSource = topList;    
    } 
} 

public class TopLevel 
{ 
    private ObservableCollection<BottomLevel> bottomList; 
    private string topName; 

    public void AddBottom() 
    { 
     bottomList.Add(new BottomLevel("B" + (1 + bottomList.Count).ToString())); 
    } 

    public TopLevel(string x) 
    { 
     bottomList = new ObservableCollection<BottomLevel>(); 
     topName = x; 
    } 

    public string TopName 
    { 
     get 
     { 
      return topName; 
     } 
     set 
     { 
      if (topName!=value) 
      { 
       topName = value; 
      } 
     } 
    } 

    public ObservableCollection<BottomLevel> BottomList 
    { 
     get 
     { 
      return bottomList; 
     } 
     set 
     { 
      if (bottomList!=value) 
      { 
       bottomList = value; 
      } 
     } 
    } 
} 

public class BottomLevel 
{ 
    private string bottomName; 

    public BottomLevel(string x) 
    { 
     bottomName = x; 
    } 

    public string BottomName 
    { 
     get 
     { 
      return bottomName; 
     } 
     set 
     { 
      if (bottomName!=value) 
      { 
       bottomName = value; 
      } 
     } 
    } 
} 
+0

是否要绑定到BottomList中的第一个BottomLevel的BottomName? {Binding BottomList/BottomName}可以在这种情况下工作。 http://www.wpftutorial.net/BindingExpressions.html – PrimeNerd

回答

1

如果你有BottomList只有一个项目,然后就可以。如果你要绑定的BottomList使用下面的代码按钮

<Button Content="{Binding Path=BottomList[0].BottomName}" Height="50"/> 

到一些列表控件,你可以绑定到DataGrid,然后你可以使用下面的代码。

<Grid x:Name="myGrid" DataContext="topList"> 
    <Border BorderBrush="AliceBlue" Grid.Column="0" BorderThickness="5"> 
     <ItemsControl x:Name="ic1"> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <StackPanel> 
         <Label Content="{Binding Path=TopName}"/> 
         <Border BorderBrush="AntiqueWhite" Grid.Column="1" BorderThickness="5"> 
          <DataGrid ItemsSource="{Binding Path=BottomList}" AutoGenerateColumns="False"> 
           <DataGrid.Columns> 
            <DataGridTemplateColumn> 
             <DataGridTemplateColumn.CellTemplate> 
              <DataTemplate> 
               <Button Content="{Binding Path=BottomName}" Height="50"/> 
              </DataTemplate> 
             </DataGridTemplateColumn.CellTemplate> 
            </DataGridTemplateColumn> 
           </DataGrid.Columns> 
          </DataGrid> 
          </Border> 
        </StackPanel> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
    </Border> 
</Grid> 

请让我知道如果你需要更多的帮助。

3

您的按钮路径不正确。 BottomList没有“名称”属性,因此您无法绑定到它。相反,只需使用BottomName作为您的路径。由于您的topList集合了“BottomLevel”,因此您需要某种嵌套的项目控件来遍历“bottomList”集合(然后使用“BottomName”作为上面的路径)。

既然这样,你主要有:

<ItemsControl //List of TopLevel> 
//Your data context is now the TopLevel item itself 
<Button Path=/> //What goes here? you have a whole collection of BottomLevel items to choose from! 
</ItemsControl> 
相关问题