1

私はC#に比較的慣れていないので、同じサイズの正方形の動的な量のグリッドを持つウィンドウを作成しようとしています. 次に、プロセスによって正方形の色が変更されます。

現在、正方形のグリッドを生成するのに苦労しています。アプリケーションを実行するたびに、リソースに夢中になっているように見えますが、その理由はわかりません。

私が使用しているコードは次のとおりです。

private void Window_Loaded(object sender, RoutedEventArgs e) {


    //create a blue brush
    SolidColorBrush vBrush = new SolidColorBrush(Colors.Blue); 

    //set the columns and rows to 100
    cellularGrid.Columns = mCellAutomaton.GAME_COLUMNS; 
    cellularGrid.Rows = mCellAutomaton.GAME_ROWS;

    //change the background of the cellular grid to yellow
    cellularGrid.Background = Brushes.Yellow;

    //create 100*100 blue rectangles to fill the cellular grid
    for (int i = 0; i < mCellAutomaton.GAME_COLUMNS; i++) {
        for (int j = 0; j < mCellAutomaton.GAME_ROWS; j++) {

            Rectangle vRectangle = new Rectangle();

            vRectangle.Width = 10;
            vRectangle.Height = 10;
            vRectangle.Fill = vBrush;

            cellularGrid.Children.Add(vRectangle);


        }
    }
}

変更可能な正方形のグリッドが必要な場合、これは私が取りたいアプローチですか?

助けてくれてありがとう、

ジェイソン

4

1 に答える 1

0

これはかなり高速に実行されるようです

   <Window x:Class="WpfApplication6.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="350" Name="UI">
        <Grid Name="Test">
            <UniformGrid Name="UniGrid" />
        </Grid>
    </Window>


/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(MainWindow_Loaded);
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        AddRows(new Size(10, 10));
    }

    private void AddRows(Size recSize)
    {
        UniGrid.Columns = (int)(UniGrid.ActualWidth / recSize.Width);
        UniGrid.Rows = (int)(UniGrid.ActualHeight / recSize.Height);
        for (int i = 0; i < UniGrid.Columns * UniGrid.Rows; i++)
        {
            UniGrid.Children.Add(new Rectangle { Fill = new SolidColorBrush(Colors.Yellow), Margin = new Thickness(1) });
        }
    }
}
于 2012-11-27T09:28:30.080 に答える