我需要创建一个带有X数量的项目的滚动查看器,我创建了一个带有图像的边界并设置为网格的代码,这是代码:
public partial class MainWindow : Window
{
panel panels = new panel();
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
int col = -1;
int actual_row =0;
for (int i = 0; i != 20;i++)
{
col++;
Grid g = panels.create_panel();
scroll.Children.Add(g);
g.SetValue(Grid.RowProperty, actual_row);
g.SetValue(Grid.ColumnProperty, col);
if (col == 3)
{
actual_row++;
col = -1;
//aad row
RowDefinition newrow = new RowDefinition();
scroll.RowDefinitions.Add(newrow);
}
}
}
}
和她是XAML:
<Window x:Class="pretyListbox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:pretyListbox"
mc:Ignorable="d"
Background="#282828"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<Style x:Key="item" TargetType="Grid">
</Style>
</Window.Resources>
<Grid>
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Standard"
>
<Grid x:Name="scroll">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
</Grid>
</ScrollViewer>
<Button Height="100" Width="100" Click="Button_Click" Content="gen" Margin="0,0,3,1" HorizontalAlignment="Right" VerticalAlignment="Bottom"/>
</Grid>
</Window>
我放了一个应用程序工作的图像:应用工作
还有滞后:滞后应用
延迟只在第一次调整大小或滚动时出现,但不是关于时间BC,我等待1分钟,它同样延迟。如果有人能告诉我如何修复或与异步方法后充电电网,我将是非常伟大的。非常感谢。
使用ItemControl并将所有项绑定到ItemSource,如果需要,它将创建垂直滚动条
请看下面的例子
<ItemsControl x:Name="CountryCodeFlagControl"
ItemsSource="{Binding SourceList}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Template>
<ControlTemplate>
<Border>
<ScrollViewer>
<ItemsPresenter />
</ScrollViewer>
</Border>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Value}">
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我解决了我的答案,我只是放了一个异步方法,现在它每次都能完美地工作。代码:
private async void Button_Click(object sender, RoutedEventArgs e)
{
int col = -1;
int actual_row =0;
for (int i = 0; i != 100; i++)
{
Task task = Task.Factory.StartNew(() =>
{
col++;
Dispatcher.Invoke(() =>
{
Grid g = panels.create_panel();
scroll.Children.Add(g);
g.SetValue(Grid.RowProperty, actual_row);
g.SetValue(Grid.ColumnProperty, col);
if (col == 3)
{
actual_row++;
col = -1;
//aad row
RowDefinition newrow = new RowDefinition();
scroll.RowDefinitions.Add(newrow);
}
});
});
await Task.Delay(100);
await task;
}
}