无法更改网格中的列数和行数

时间:2017-07-27 12:32:09

标签: c# wpf xaml

我尝试制作一个网格,您可以使用滑块调整行数和列数。我的代码中没有语法错误,但它没有按照我的意愿行事。网格和滑块具有以下XAML代码:

<Window x:Class="Neural_Network_Investigation.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:Neural_Network_Investigation"
    mc:Ignorable="d"
    Title="MainWindow"
    Height="Auto"
    MaxHeight="520" MinHeight="450"
    Width="Auto" 
    MaxWidth="700" MinWidth="630">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="300"/>
            <RowDefinition Height="100"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="300"/>
            <ColumnDefinition Width="300"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Border Grid.Row="0"
                Grid.Column="0"
                Grid.RowSpan="4"
                Grid.ColumnSpan="4" Background="Gray"/>
        <Grid Grid.Column="1"
              Grid.Row="1"
              x:Name="NetworkGrid"
              ShowGridLines="True"/>
        <StackPanel Grid.Column="1"
                    Grid.Row="2"
                    Orientation="Vertical"
                    HorizontalAlignment="Left"
                    VerticalAlignment="Center">
            <StackPanel Orientation="Horizontal">
                <TextBlock FontSize="12"
                           Text="Grid Size: "/>
                <TextBlock x:Name="GridSizeText"
                           FontSize="12" Text=""
                           MinWidth="15"/>
                <Slider x:Name="GridSizeSlider"
                        Minimum="4" Maximum="20"
                        TickPlacement="BottomRight"
                        TickFrequency="1"
                        IsSnapToTickEnabled="True"
                        Width="100"
                        ValueChanged="GridSizeSlider_ValueChanged"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">

            </StackPanel>
        </StackPanel>
    </Grid>
</Window>

更改滑块后,我希望更新NetworkGrid中的行数和列数以匹配:

    private void GridSizeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        var GridSize = (int)GridSizeSlider.Value;
        GridSizeText.Text = GridSize.ToString();
        NetworkGrid.ColumnDefinitions.Clear();
        NetworkGrid.RowDefinitions.Clear();

        double SquareWidth = NetworkGrid.Width / GridSize;
        double SquareHeight = NetworkGrid.Height / GridSize;

        for(int columnNumber = 0; columnNumber < GridSize; columnNumber++)
        {
            var column = new ColumnDefinition();
            NetworkGrid.ColumnDefinitions.Add(column);

            for (int rowNumber = 0; rowNumber < GridSize; rowNumber++)
            {
                var row = new RowDefinition();
                NetworkGrid.RowDefinitions.Add(row);

                var square = new Border();
                square.Background = Brushes.Black;
                square.SetValue(Grid.ColumnProperty, columnNumber);
                square.SetValue(Grid.RowProperty, rowNumber);
            }
        }
    }

当我运行程序时,尽管启用了ShowGridLines,但我什么也看不见。这意味着它不会将列和行添加到网格中。我该如何解决这个问题?

我使用过本文中的代码:How to add rows and columns to a WPF Grid programmatically

我还检查了this blog,展示了一个类,它允许你绘制一个动态行数或列数的网格,但是我无法理解如何在我自己的程序中使用该类。 / p>

0 个答案:

没有答案