Создание строк кодом С#

Как используя код С# создавать строки вместо необходимости писать через XAML <RowDefinition/> огромное ~400 раз?

<Window x:Class="Proj1.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:col="clr-namespace:System.Collections;assembly=mscorlib"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    
    mc:Ignorable="d"
    Title="Main Window" Height="450" Width="800">


<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="18"/>
        <RowDefinition/>
        <RowDefinition Height="17"/>
    </Grid.RowDefinitions>
    <Border BorderBrush="Gray" BorderThickness="1">
        <Grid HorizontalAlignment ="Left" VerticalAlignment="Top" Grid.Row="0">
            <Menu IsMainMenu="True">
                <MenuItem Header="Open"/>
                <MenuItem Header="New"/>
            </Menu>
        </Grid>
    </Border>
    <Grid ShowGridLines="True" Grid.Row="1">
        <ScrollViewer Grid.ColumnSpan="2" Grid.Row="1" HorizontalScrollBarVisibility="Visible">
            <Grid Height="1000" Width="1000" ShowGridLines="True">
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="80"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                
                <Grid Grid.Column="0" Grid.RowSpan="1111">

                </Grid>
                <Grid Grid.Column="1">

                </Grid>
            </Grid>
        </ScrollViewer>
    </Grid>
</Grid>

введите сюда описание изображения


Ответы (1 шт):

Автор решения: aepot

Нужно создать ItemsControl, засунуть его в ScrollViwer, и привязать к коллекции.

<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="WPF Window" Height="600" Width="1000" Loaded="Window_Loaded">
    <Grid>
        <ScrollViewer VerticalScrollBarVisibility="Auto">
            <ItemsControl ItemsSource="{Binding Rows}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid SnapsToDevicePixels="True">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="60"/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <Border Background="LightGray">
                                <TextBlock Padding="10,5" HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding Title}"/>
                            </Border>
                            <Border Grid.ColumnSpan="2" BorderThickness="0,0,0,1">
                                <Border.BorderBrush>
                                    <VisualBrush>
                                        <VisualBrush.Visual>
                                            <Rectangle StrokeDashArray="4 4" Stroke="Black" StrokeThickness="1"
                                                Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=ActualWidth}"
                                                Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=ActualHeight}"/>
                                        </VisualBrush.Visual>
                                    </VisualBrush>
                                </Border.BorderBrush>
                            </Border>
                            <ItemsControl Grid.Column="1" ItemsSource="{Binding Sounds}">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <Rectangle Fill="Green" Width="{Binding Length}" Height="{Binding ActualHeight, RelativeSource={RelativeSource AncestorType=Canvas}}" HorizontalAlignment="Left"/>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <Canvas Background="AliceBlue" Margin="0,0,0,1" />
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemContainerStyle>
                                    <Style>
                                        <Setter Property="Canvas.Left" Value="{Binding Start}" />
                                        <Setter Property="Canvas.Top" Value="0" />
                                    </Style>
                                </ItemsControl.ItemContainerStyle>
                            </ItemsControl>
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </ScrollViewer>
    </Grid>
</Window>
public partial class MainWindow : INotifyPropertyChanged
{
    private ObservableCollection<RowData> _rows;

    public ObservableCollection<RowData> Rows
    {
        get => _rows;
        set
        {
            _rows = value;
            OnPropertyChanged();
        }
    }

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        Random rnd = new Random();
        Rows = new ObservableCollection<RowData>();
            
        for (int i = 0; i < 400; i++)
            Rows.Add(new RowData()
            {
                Title = "Item" + i,
                Sounds = new ObservableCollection<SoundSquare>(Enumerable.Range(0, 10).Select(j => new SoundSquare() { Start = j * 80 + (i % 2 * 40), Length = rnd.Next(80) }))
            });
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public class RowData
{
    public string Title { get; set; }
    public ObservableCollection<SoundSquare> Sounds { get; set; }
}

public class SoundSquare
{
    public int Start { get; set; }
    public int Length { get; set; }
}

введите сюда описание изображения

→ Ссылка