2016-11-24 203 views
1

我想建立这样一个的UserInterface(用于视频编辑时间轴) enter image description hereC#数据绑定嵌套的ItemsControl WPF

,我有所有的类:

public class Timeline 
    { 
     public Timeline() 
     { 
      groups = new ObservableCollection<Group>(); 
     } 
     public ObservableCollection<Group> groups { get; set; } 

    } 

public class Group 
{ 
    public string Name { get; set; } 

    public TrackGroup() 
    { 
     tracks = new List<Track>(); 
    } 

    public List<Track> tracks { get; set; } 
} 

public class Track 
{ 
    public Track() 
    { 
     units= new ObservableCollection<Unit>(); 
    } 
    public ObservableCollection<Unit> units { get; set; } 
} 

public class Unit 
{ 
    public string unitName { get; set; } 
} 

而且在主要我有这:

public Timeline timeline = new Timeline(); 
public Unit unit = new Unit(); 
public Track track = new Track(); 
public Group group = new Group(); 
track.units.Add(unit); 
group.tracks.Add(track); 
timeline.groups.Add(group); 

而且在Main.xaml(不完整的,因为它不工作)

<ListBox 
     HorizontalContentAlignment="Stretch" 
     ItemsSource="{Binding timeline.groups}"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <DockPanel LastChildFill="True"> 
          <ItemsControl ItemsSource="{Binding timeline.groups.tracks}"> 
           <ItemsControl.ItemTemplate > 
            <ItemsControl ItemsSource="{Binding timeline.groups.tracks.units}"> 
             <ItemsControl.ItemTemplate > 
             </ItemsControl.ItemTemplate > 
            </ItemsControl> 
           </ItemsControl.ItemTemplate> 
          </ItemsControl> 
         </DockPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 

如何绑定每个itemssource?因为时间轴,,轨道所有列表,所以我需要在ItemsControl中显示它们。我很困惑

回答

2

对于内部列表中的绑定,您不需要使用“完整”路径。

因为当你绑定一些ItemControl并尝试风格它的项目请记住,每个项目已经是你作为ItemsSource

EDIT1收集的例子:
Window.xaml.cs文件:

Timeline timeline = new Timeline(); 
Unit unit1 = new Unit() {unitName = "1"}; 
Unit unit2 = new Unit() {unitName = "2"}; 
Track track = new Track(); 
Group group = new Group(); 
track.units.Add(unit1); 
track.units.Add(unit2); 
group.tracks.Add(track); 
timeline.groups.Add(group); 
list.DataContext = timeline; 

这里我没有使用虚拟机,如果你想使用虚拟机只是将它添加到你的窗口的数据上下文并绑定列表。 Xaml:

<ListBox HorizontalContentAlignment="Stretch" 
      Name="list" 
      ItemsSource="{Binding groups}"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <DockPanel LastChildFill="True"> 
        <ItemsControl ItemsSource="{Binding tracks}"> 
         <ItemsControl.ItemTemplate > 
          <DataTemplate> 
           <ItemsControl ItemsSource="{Binding units}"> 
            <ItemsControl.ItemTemplate> 
             <DataTemplate> 
              <TextBox Text="{Binding unitName}"/> 
             </DataTemplate> 
            </ItemsControl.ItemTemplate> 
           </ItemsControl> 
          </DataTemplate> 
         </ItemsControl.ItemTemplate> 
        </ItemsControl> 
       </DockPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

只是风格,测试它。如果您将使用VM与Timeline属性更改绑定到'timelinePropertyName'.groups

并阅读有关绑定的更多信息。

+0

它不适用于“绑定timeline.groups”。我在主类中使用“datacontext = this”,是不是? –

+0

已更新的答案。 – Shakra

0

尝试大写相应的成员。 WPF区分大小写。