在XAML中显示矩阵

时间:2013-08-05 13:36:15

标签: c# wpf xaml

我正在研究一个计算几个4x4矩阵(2D双阵列)的应用程序。

private double[,] matrix1 = new double[4,4];
private double[,] matrix2 = new double[4,4];

计算系数后,我想在我的视图中显示矩阵。我想使用方括号(http://upload.wikimedia.org/math/e/2/b/e2b3fba5aaeb1ade3407ca02aa870a5e.png)或大括号(http://upload.wikimedia.org/math/5/d/5/5d5847e4483c97c3f4c767ed15ab1c27.png)。
XAML是否提供了以这种方式显示矩阵的元素?如果没有,是否可以以类似的方式显示矩阵?

2 个答案:

答案 0 :(得分:1)

如有疑问,请假装它。这可能对您有用,可以作为获得您所效果的简单方法。实施例;

<Grid VerticalAlignment="Center" HorizontalAlignment="Center" MaxWidth="200">
   <Rectangle Stroke="Black" StrokeThickness="2" Fill="White"/>
   <Rectangle Fill="White" Margin="8,0"/>

   <TextBlock TextWrapping="Wrap" Margin="10">
      <Run Text="This would be all of your content here."/><LineBreak/><LineBreak/>
      <Run Text="If you just populate your content like this, then you could just put a Center Horizontal/Vertical Alignment on the Grid and it would only take up the space necessary."/>
   </TextBlock>

</Grid>

希望这有帮助。

答案 1 :(得分:1)

在WPF中,您可以使用Path个对象轻松创建此外观以绘制边(这些是直的)和UniformGrid来显示内容:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="300">
    <Window.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="TextAlignment" Value="Center" />
            <Setter Property="VerticalAlignment" Value="Center" />
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Path Grid.Column="0" Data="M 0,0 0,300 15,300 15,295 5,295 5,5 15,5 15,0" 
            Width="15" Height="300" Fill="Black" VerticalAlignment="Stretch" 
            HorizontalAlignment="Center" />
        <UniformGrid Grid.Column="1" Columns="2" Rows="2" TextElement.FontFamily=
            "Palatino Linotype" TextElement.FontSize="28">
            <TextBlock>
                <Run Text="a" />
                <Run Typography.Variants="Subscript" FontStyle="Italic" Text="11" />
            </TextBlock>
            <TextBlock>
                <Run Text="a" />
                <Run Typography.Variants="Subscript" FontStyle="Italic" Text="11" />
            </TextBlock>
            <TextBlock>
                <Run Text="a" />
                <Run Typography.Variants="Subscript" FontStyle="Italic" Text="21" />
            </TextBlock>
            <TextBlock>
                <Run Text="a" />
                <Run Typography.Variants="Subscript" FontStyle="Italic" Text="22" />
            </TextBlock>
        </UniformGrid>
        <Path Grid.Column="2" Data="M 15,0 15,300 0,300 0,295 10,295 10,5 0,5 0,0" 
            Width="15" Height="300" Fill="Black" VerticalAlignment="Stretch" 
            HorizontalAlignment="Center" />
    </Grid>
</Window>

有两件重要事项需要注意才能正常工作:

您选择的TextElement.FontFamily 必须才能显示Subscript

您可以绑定到Run.Text属性,而不是像我在示例中那样硬编码值:

<Run Text="{Binding Value1}" />
<Run Typography.Variants="Subscript" FontStyle="Italic" Text="{Binding Value2}" />