簡體   English   中英

數據綁定幫助-WPF

[英]Data Binding Help - WPF

我有這兩節課:

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();

我想這樣顯示:

一切都將在ListBox中:我希望將managerOBJ.Groups綁定到該ListBox。 - 怎么做? 比我想創建DataTamplate來顯示每個組和該組中的所有鏈接。 - 怎么做?

我想從XAML盡可能多地做

更新:

這就是我得到的。 這不是工作。 列表框為空。

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

在隱藏的代碼中,我有:

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

整個管理器都包含在一個列表框中,對於管理器中的每個下載組,您都添加一個itemscontrol,其中包含另一個帶有鏈接的items控件。 這可以通過使用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>

在代碼中,您應該輸入:

Manager managerOBJ = new Manager();
myGroups.DataContext = managerOBJ;
  1. managerOBJ定義為視圖managerOBJ中的屬性
  2. 將viewmodel綁定到您的視圖。
  3. 將ListBox項目itemssource綁定到managerOBJ.Groups
  4. 在ListBox內定義DataTemplate以顯示每個DownloadGroup

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM