這是我目前正在研究的項目中的一段代碼。它使用UWP社區工具包中的Bladeview Control和WrapPanel Panel(如果您還沒有它,請使用nuget將其添加到您的項目中)以創建我相信您將使用動態生成的畫布的效果。
如果您還沒有,我還建議從Windows應用商店下載「UWP示例應用程序」。它演示了社區工具包可用的控件併爲它們提供了代碼。它還提供代碼和文檔,以提供您可能需要的許多有用功能。
該示例應用程序還將爲您演示我正在討論的bladeview控件。
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ScrollViewer Height="764" Width="776" HorizontalScrollMode="Disabled" VerticalScrollMode="Enabled" HorizontalScrollBarVisibility="Disabled">
<controls:BladeView x:Name="UserBladeView" BladeMode="Normal" ItemsSource="{x:Bind UserDataBase}" Height="auto" Width="767" > <!-- ItemSource Should be the Observable Collection containing your user objects -->
<controls:BladeView.ItemsPanel>
<ItemsPanelTemplate>
<wrapPanel:WrapPanel Orientation="Horizontal" Height="auto" Width="765" Margin="5"/>
</ItemsPanelTemplate>
</controls:BladeView.ItemsPanel>
<controls:BladeView.ItemTemplate>
<DataTemplate x:DataType="models:Product"> <!-- This should correspond to the namespace the class that represents your users resides in xmlns:local="using:yournamespace"-->
<controls:BladeItem x:Name="UserTiles"
IsOpen="True"
TitleBarVisibility="Collapsed" Width="365" Height="575" Visibility="Visible" Background="#FF1D1919" BorderBrush="#FF5D5D5D" BorderThickness="2" Margin="5">
<StackPanel HorizontalAlignment="Left" Height="auto" VerticalAlignment="Top" Width="371" Margin="0,0,-10,-4" >
<TextBlock Margin="2,2,0,2" HorizontalAlignment="Left" TextWrapping="Wrap" x:Name="UserFirstName" Text="{Binding FirstName}" VerticalAlignment="Top" Height="auto" Width="308" FontSize="36" Foreground="#FFCDCDCD"/>
<TextBlock Margin="2" HorizontalAlignment="Left" TextWrapping="Wrap" x:Name="UserLastName" Text="{Binding LastName}" VerticalAlignment="Top" Height="50" Width="316" FontSize="18" Foreground="#FFCDCDCD" Visibility="Visible"/>
<TextBlock Margin="2" HorizontalAlignment="Left" TextWrapping="Wrap" x:Name="UserTitle" Text="{Binding Title}" VerticalAlignment="Top" Height="50" Width="316" FontSize="18" Foreground="#FFCDCDCD" Visibility="Visible"/>
<Image Margin="5" x:Name="UserImage" Source="{Binding UserHeadShot}" Height="100" Width="auto" />
</StackPanel>
</controls:BladeItem>
</DataTemplate>
</controls:BladeView.ItemTemplate>
</controls:BladeView>
</ScrollViewer>
</Grid>
的BladeView控制範圍內的ItemSource應該是持有的ObservableCollection您的用戶對象和BladeItem內您的綁定應該代表要顯示的屬性。
這會做的是創建一個BladeItem,其中包含集合中每個對象的類中的所有綁定屬性。 WrapPanel將確保BladeViewer在必要時顯示一個新的BladeItems行。
爲什麼不使用'GridView'並使用上面的canvas創建一個'DataTemplate',綁定從DataBase中檢索的數據,它會重複它自己? – AVK