【问题标题】:GridView or Grid Windows 8.1GridView 或网格 Windows 8.1
【发布时间】:2015-04-30 18:48:16
【问题描述】:

我正在尝试在 Windows 8 通用应用程序中创建一个简单的网格。 以下是我的目标。

Row1Col1 Row1Col2 Row1Col3

Row2Col1 Row2Col2 Row2Col3

Row3Col1 Row3Col2 Row3Col3

<Grid x:Name="Grid1" HorizontalAlignment="Left" Height="217" Margin="557,135,0,0" Grid.Row="1" VerticalAlignment="Top" Width="433">
        <Grid.RowDefinitions>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions>
        <Grid.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="Black" Offset="0"/>
                <GradientStop Color="White" Offset="1"/>
            </LinearGradientBrush>
        </Grid.Background>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
    </Grid>

我想在 XAML 中设置均匀间隔的行和列,并在代码中添加文本标签。

类似:

Grid.Row1.Col1.Text="Row1Col1"; Grid.Row1.Col2.Text="Row1Col2";

一个简短的 XAML sn-p 和一段 c# 代码会很有帮助。

到目前为止,这是我的 xaml。

任何帮助表示赞赏。

【问题讨论】:

    标签: c# windows xaml gridview


    【解决方案1】:

    为了向网格中的不同单元格添加文本,我添加了 TextBlocks 并适当地放置它们:

    <Page.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="FontSize" Value="40"/>
        </Style>
    </Page.Resources>
    
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
        <TextBlock Name="r1c1" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        <TextBlock Name="r1c2" Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        <TextBlock Name="r1c3" Grid.Row="0" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        <TextBlock Name="r2c1" Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        <TextBlock Name="r2c2" Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        <TextBlock Name="r2c3" Grid.Row="1" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        <TextBlock Name="r3c1" Grid.Row="2" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        <TextBlock Name="r3c2" Grid.Row="2" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        <TextBlock Name="r3c3" Grid.Row="2" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Center"/>
    </Grid>
    

    例如,我将后面的代码更改为以下内容:

    private int N = 3;
    private TextBlock[,] gridText;
    public MainPage()
    {
        this.InitializeComponent();
        InitializeGridText();
    
        MethodThatChangesText();
    }
    
    private void InitializeGridText()
    {
        gridText = new TextBlock[N, N];
        gridText[0, 0] = r1c1;
        gridText[0, 1] = r1c2;
        gridText[0, 2] = r1c3;
        gridText[1, 0] = r2c1;
        gridText[1, 1] = r2c2;
        gridText[1, 2] = r2c3;
        gridText[2, 0] = r3c1;
        gridText[2, 1] = r3c2;
        gridText[2, 2] = r3c3;
    }
    
    void MethodThatChangesText()
    {
        // Some Logic Here
    
        for(int i = 0; i < N; i++)
            for(int j = 0; j < N; j++)
                gridText[i, j].Text = String.Format("Row{0}Col{1}", i + 1, j + 1);
    }
    

    根据您要执行的操作,您可以添加自己的逻辑并调用此方法以响应某些事件(例如按钮单击...)。

    【讨论】:

    • 谢谢弗兰克。我希望以更直接的方式将文本分配给网格,但现在您的解决方案有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    相关资源
    最近更新 更多