简体   繁体   English

如何从列表字符创建DataGrid矩阵?

[英]How to create DataGrid Matrix from List Chars?

I display a List with chars in a TextBox , but the spacing is not uniform. 我在TextBox显示带charsList ,但间距不统一。

ABCDE ABCDE
FGHIJ FGHIJ
KLMNO KLMNO
PQRST PQRST
UVWXY UVWXY


How can I bind it to a WPF DataGrid so it displays like a uniform 5x5 Matrix? 如何将其绑定到WPF DataGrid ,使其显示为统一的5x5矩阵?

Or is there another way to make it uniform? 还是有其他方法可以使其统一?

A B C D E  
F G H I J  
K L M N O  
P Q R S T  
U V W X Y

C# C#

public char A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y;

public List<char> myList = new List<char>()
{
    A, B, C, D, E,  
    F, G, H, I, J,  
    K, L, M, N, O,  
    P, Q, R, S, T,  
    U, V, W, X, Y
}

public ObservableCollection<char> myCollection = new ObservableCollection<char>(myList);

XAML XAML

<DataGrid x:Name="dataGrid" 
          HorizontalAlignment="Left" 
          Height="299" 
          Margin="191,399,0,0" 
          VerticalAlignment="Top" 
          Width="360"/>

Besides the possibility to use a monospaced font, like 除了可以使用等宽字体外,例如

<TextBlock FontFamily="Lucida Console">
    <Run>ABCDE</Run>
    <LineBreak/>
    <Run>FGHIJ</Run>
    <LineBreak/>
    <Run>KLMNO</Run>
    <LineBreak/>
    <Run>PQRST</Run>
    <LineBreak/>
    <Run>UVWXY</Run>
</TextBlock>

you could use an ItemsControl with a UniformGrid: 您可以将ItemsControl与UniformGrid一起使用:

<ItemsControl ItemsSource="ABCDEFGHIJKLMNOPQRSTUVWXY">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns="5"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" TextAlignment="Center"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Although the XAML designer complains about the ItemsSource property, it will work because a string is an IEnumerable and the Text Binding automatically converts char to string . 尽管XAML设计人员抱怨ItemsSource属性,但是它可以工作,因为字符串是IEnumerable,并且文本绑定自动将char转换为string

You may of course also set the ItemsSource in code behind: 您当然也可以在后面的代码中设置ItemsSource:

itemsControl.ItemsSource = new List<string>()
{
    "A", "B", "C", "D", "E",
    "F", "G", "H", "I", "J",
    "K", "L", "M", "N", "O",
    "P", "Q", "R", "S", "T",
    "U", "V", "W", "X", "Y"
};

or 要么

itemsControl.ItemsSource = new List<char>()
{
    'A', 'B', 'C', 'D', 'E',
    'F', 'G', 'H', 'I', 'J',
    'K', 'L', 'M', 'N', 'O',
    'P', 'Q', 'R', 'S', 'T',
    'U', 'V', 'W', 'X', 'Y'
};

or of course 或当然

itemsControl.ItemsSource = "ABCDEFGHIJKLMNOPQRSTUVWXY";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM