0

我在边框内有一个 TextBox 网格,内容绑定如下

<Border BorderBrush="White" BorderThickness="1" Margin="0">
    <TextBlock TextWrapping="Wrap" FontSize="29.333" Text="{Binding TextArray[0][0], Mode=OneWay}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0"/>
</Border>
<Border BorderBrush="White" BorderThickness="1" Margin="0" Grid.Column="1">
    <TextBlock TextWrapping="Wrap" FontSize="29.333" Text="{Binding TextArray[0][1], Mode=OneWay}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0"/>
</Border>
<Border BorderBrush="White" BorderThickness="1" Margin="0" Grid.Column="2">
    <TextBlock TextWrapping="Wrap" FontSize="29.333" Text="{Binding TextArray[0][2], Mode=OneWay}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0"/>
</Border>
<Border BorderBrush="White" BorderThickness="1" Margin="0" Grid.Row="1" Grid.Column="0">
    <TextBlock TextWrapping="Wrap" FontSize="29.333" Text="{Binding TextArray[1][0], Mode=OneWay}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0"/>
</Border>
<Border BorderBrush="White" BorderThickness="1" Margin="0" Grid.Row="1" Grid.Column="1">
    <TextBlock TextWrapping="Wrap" FontSize="29.333" Text="{Binding TextArray[1][1], Mode=OneWay}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0"/>
</Border>
<Border BorderBrush="White" BorderThickness="1" Margin="0" Grid.Row="1" Grid.Column="2">
    <TextBlock TextWrapping="Wrap" FontSize="29.333" Text="{Binding TextArray[1][2], Mode=OneWay}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0"/>
</Border>
<Border BorderBrush="White" BorderThickness="1" Margin="0" Grid.Row="2" Grid.Column="0">
    <TextBlock TextWrapping="Wrap" FontSize="29.333" Text="{Binding TextArray[2][0], Mode=OneWay}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0"/>
</Border>
<Border BorderBrush="White" BorderThickness="1" Margin="0" Grid.Row="2" Grid.Column="1">
    <TextBlock TextWrapping="Wrap" FontSize="29.333" Text="{Binding TextArray[2][1], Mode=OneWay}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0"/>
</Border>
<Border BorderBrush="White" BorderThickness="1" Margin="0" Grid.Row="2" Grid.Column="2">
    <TextBlock TextWrapping="Wrap" FontSize="29.333" Text="{Binding TextArray[2][2], Mode=OneWay}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0"/>
</Border>

这对于小网格来说很好,但是对于大网格会有很多复制粘贴和替换值。有一个更好的方法吗?

4

2 回答 2

2

为什么不使用 DataGrid for WinRT - 有很多来自第三方的可用于 WinRT 的数据网格,例如 Syncfusion 提供,您可以在此处查看。

http://darkcore.in/getting-started-in-syncfusions-datagrid-sfdatagrid-for-winrt/

于 2013-06-08T14:32:24.780 回答
2

简单的解决方案,停止使用那些疯狂的 TextArrays。让我们假设您确实有一个像您展示的那样的数组。为什么不让你的生活更轻松?

从简化数据开始!

string[][] _Data = new[] 
    { 
        new string[] { "A", "B", "C" }, 
        new string[] { "1", "2", "3" } 
    };
var _NewData =
    from d in _Data
    select new
    {
        One = d[0],
        Two = d[1],
        Three = d[2]
    };
this.DataContext = _NewData;

然后您可以简化您的 XAML:

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <ItemsControl ItemsSource="{Binding}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid Width="600">

                    <Grid.Resources>
                        <Style TargetType="Border">
                            <Setter Property="BorderBrush" Value="White" />
                            <Setter Property="BorderThickness" Value="1" />
                            <Setter Property="Margin" Value="0" />
                        </Style>
                        <Style TargetType="TextBlock">
                            <Setter Property="TextWrapping" Value="Wrap" />
                            <Setter Property="FontSize" Value="29.333" />
                            <Setter Property="HorizontalAlignment" Value="Center" />
                            <Setter Property="VerticalAlignment" Value="Center" />
                            <Setter Property="Margin" Value="0" />
                        </Style>
                    </Grid.Resources>

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>

                    <!-- first record -->
                    <Border Grid.Column="0">
                        <TextBlock Text="{Binding One}" />
                    </Border>

                    <!-- second record -->
                    <Border Grid.Column="1">
                        <TextBlock Text="{Binding Two}" />
                    </Border>

                    <!-- third record -->
                    <Border Grid.Column="2">
                        <TextBlock Text="{Binding Three}" />
                    </Border>

                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

现在有零冗余。XAML 本来就是要使用的。

祝你好运!

于 2013-06-08T03:26:16.303 回答