我想构建一个像这样的UserInterface(视频编辑器的时间轴)
我有所有课程:
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; }
}
在Main我有这个:
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中显示它们。我很困惑
答案 0 :(得分: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;
这里我没有使用VM,如果你想使用VM只是将它添加到窗口的数据上下文并将列表绑定到它。 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
阅读有关绑定的更多信息。
答案 1 :(得分:0)
尝试大写相应的成员。 WPF区分大小写。