简体   繁体   English

C#数据绑定嵌套ItemsControl WPF

[英]C# data binding nested ItemsControl wpf

I want to build a UserInterface like this (a timeline for a video editor) 我想构建一个像这样的UserInterface(视频编辑器的时间轴) 在此处输入图片说明

and I have all classes: 我有所有课程:

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; }
}

And in Main I have this: 在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);

And in Main.xaml(not complete because it doesn't work) 并且在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>

How can I binding each itemssource? 如何绑定每个项目源? because timeline , group , track all are list, so I need to show them in ItemsControl. 因为timelinegrouptrack都是列表,所以我需要在ItemsControl中显示它们。 I was very confused 我很困惑

You don't need to use 'full' path for bindings at inner lists. 您不需要在内部列表中使用“完整”路径进行绑定。

Because when you bind some ItemControl and try to style it's item remember that each item already an example of collection that you used as ItemsSource 因为当您绑定某些ItemControl并尝试为其设置样式时,请记住,每个项目已经是用作ItemsSource的集合的示例

Edit1: EDIT1:
Window.xaml.cs file: 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;

here i didn't use VM if you want to use VM just add it to data context of your window and bind list to it. 如果要使用VM,在这里我没有使用VM,只需将其添加到窗口的数据上下文中并绑定列表即可。 Xaml: 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>

Just style, tested it. 只是样式,测试一下。 If you'll use VM with Timeline property change binding to 'timelinePropertyName'.groups 如果您将VM与Timeline属性一起使用,请将绑定更改为'timelinePropertyName'.groups

And read more about bindings. 并了解有关绑定的更多信息。

Try capitalizing the appropriate members. 尝试大写适当的成员。 WPF is case sensitive. WPF区分大小写。

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

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