开发者

Data Binding Help - WPF

开发者 https://www.devze.com 2023-02-18 15:02 出处:网络
I have those two classes: class DownloadLink { public string Name { get; private set; } public string Url { get; private set; }

I have those two classes:

class DownloadLink
{
    public string Name { get; private set; }
    public string Url { get; private set; }
    //(...)
}

class DownloadGroup
{        
    public List<DownloadLink> Links { get; private set; }
    //(...)
}

class Manager
{
    public List<DownloadGroup> Groups { get; private set; }
}

Manager managerOBJ = new Manager();

I want to display this like that:

Everything will be in ListBox: I wan to bind managerOBJ.Groups to that ListBox. - How to do it? Than I want to create DataTamplate to display each group and all links in that group. - How to do it?

I want to do as much as possible from XAML

UPDATE:

This is what I got. It's not workig. List box is empty.

    <ListBox DockPanel.Dock="Right" VerticalAlignment="Stretch" Width="500" HorizontalAlignment="Right" Background="#FFE1FFF5" HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Visible" ItemsSource="{Binding Path=Groups}" Name="GroupsListBox">
        <ListBox.ItemTemplate>
            <DataTemplate>
        开发者_StackOverflow社区        <StackPanel Height="30" VerticalAlignment="Top" Width="500" >
                    <Grid Height="Auto" Width="500">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Label Content="XX MB w XX plikach" HorizontalAlignment="Stretch" Margin="0"/>
                    </Grid>
                    <ListBox HorizontalAlignment="Stretch" Height="43" Margin="0,5,0,0" Width="Auto" VerticalAlignment="Top" ItemsSource="{Binding Path=Links}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Label Content="{Binding Path=Name}" />
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

and in code behid I have:

RapideoAccount = new Rapideo();
GroupsListBox.DataContext = RapideoAccount;


The whole manager is contained in a listbox, for each downloadgroup in the manager you add an itemscontrol that contains another items control with the links in it. This can be done by using DataTemplates:

    <ListBox Name="myGroups"
         ItemsSource="{Binding Path=Groups}">
    <!-- each List<DownloadGroup> in the manager: -->
    <ListBox.ItemTemplate>
        <DataTemplate>
            <ItemsControl ItemsSource="{Binding Path=Links}">
                <!-- each Link in the Downloadgroup -->
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding Path=Name}" />
                            <TextBlock Text="{Binding Path=Url}" />
                        </StackPanel>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

In code you would put:

Manager managerOBJ = new Manager();
myGroups.DataContext = managerOBJ;


  1. define managerOBJ as a property in your viewmodel
  2. binding viewmodel to your view.
  3. binding ListBox itemssource to managerOBJ.Groups.
  4. define DataTemplate inside ListBox to display each DownloadGroup.
0

精彩评论

暂无评论...
验证码 换一张
取 消