如何将网格动态添加到网格

时间:2019-07-15 14:44:23

标签: c# .net wpf binding grid

对于我当前的项目,我想制作一个UI,其中信息显示在“平铺”上,我将其设计为带有其他Label和进度条的网格。但是我不知道如何在我的主网格上显示这些图块。

我已经尝试过Gridview,但它与网络上的示例不兼容,并且要想在没有网格的情况下做起来并不容易。

这是可根据需要插入多次的网格

<Grid Margin="10" Background="AntiqueWhite" Grid.Row="0" Grid.Column="0">
            <Label Content="{Binding fieldname}" FontFamily="Arial Black" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,20,0,0"/>
            <Label Content="{Binding datebegin}" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="85,85,0,0" FontFamily="Arial Black" FontSize="18" />
            <Label Content="{Binding dateend}" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="285,85,0,0" FontFamily="Arial Black" FontSize="18" />
             <ProgressBar Margin="0,150,0,0" Value="{Binding progression}" Background="White" BorderBrush="Black" VerticalAlignment="Top" Height="50" />
            </Grid>

我需要一种将产生的精确图块添加到网格中的方法(或其他可以进入具有3列且无限​​制行的滚动查看器的任何方法)。

1 个答案:

答案 0 :(得分:0)

下面是xmal。我已经使用ItemsControl和WrapPanel作为ItemsPanelTemplate来包装ItemsControl的项目

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Button Content="Add" Height="26" Width="75" Command="{Binding Add}" Grid.Row="0"/>
    <ScrollViewer Height="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.Row="1">
        <ItemsControl ItemsSource="{Binding Items}" HorizontalAlignment="Left">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border Margin="10,10,0,0" BorderBrush="Black" BorderThickness="1">
                        <StackPanel Width="200" Height="Auto">
                            <Label Content="{Binding fieldname}" FontFamily="Arial Black" FontSize="20" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,10,0,0"/>
                            <Label Content="{Binding datebegin}" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,10,0,0" FontFamily="Arial Black" FontSize="18" />
                            <Label Content="{Binding dateend}" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,10,0,0" FontFamily="Arial Black" FontSize="18" />
                            <ProgressBar Value="{Binding progression}" Margin="5,10,5,10" Background="White" BorderBrush="Black" VerticalAlignment="Top" Height="20" />
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </ScrollViewer>
</Grid>

视图模型为

public class Item
{
    public string fieldname
    {
        get;
        set;
    }

    public string datebegin
    {
        get;
        set;
    }

    public string dateend
    {
        get;
        set;
    }

    public string progression
    {
        get;
        set;
    }

    public Item(int number)
    {
        fieldname = "Filed name " + number;
        datebegin = "12-12-12";
        dateend = "14-12-12";
        progression = (5 * number).ToString();
    }
}

public class ViewModel
{
    public ICollection<Item> Items
    {
        get;
        private set;
    }

    public ViewModel()
    {
        Items = new ObservableCollection<Item>();
    }

    public ICommand Add
    {
        get
        {
            return new RelayCommand((a) =>
            {
                Items.Add(new Item(Items.Count));
            });
        }
    }
}

中继命令

public class RelayCommand : ICommand
{
    private Action<object> execute;
    private Predicate<object> canExecute;
    public RelayCommand(Action<object> execute, Predicate<object> canExecute = null)
    {
        this.execute = execute;
        this.canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        return canExecute == null || canExecute(parameter);
    }

    public void Execute(object parameter)
    {

        execute(parameter);
    }

    public event EventHandler CanExecuteChanged
    {
        add
        {
            CommandManager.RequerySuggested += value;
        }
        remove
        {
            CommandManager.RequerySuggested -= value;
        }
    }