如何设置不同的绑定到每个数组元素? C#UWP [英] How to set different Binding to each Array element? C# UWP

查看:254
本文介绍了如何设置不同的绑定到每个数组元素? C#UWP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法显示我的TextBlock的阵列 - 每一个[元]在单独的行,但我想将每个数组[索引]元素成单独的TextBlock在同一行,只要它来自同一直线上,所以

I managed to display my array in TextBlock - each [element] in separate row, but I would like to put each Array[index] element into separate TextBlock in the same Row, as long as it comes from same line, So:


  1. 系统文件被访问

  2. 行进行排序

  3. 行被拆分成词

  4. 每行放在单独的行

5。从线每个字被放入单独的TextBlock,但在同一行中。

下面是我的code:

public async void ReadFile()
    {
        var path = @"CPU.xls";
        var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;

        var file = await folder.GetFileAsync(path);
        var readFile = await Windows.Storage.FileIO.ReadLinesAsync(file);
        foreach (string line in readFile.OrderBy(line =>
        {
            int lineNo;
            var success = int.TryParse(line.Split(';')[4], out lineNo);
            if (success) return lineNo;
            return int.MaxValue;
        }))
        {
            string[] splitLines = line.Split(';');

            for (int index = 0; index < splitLines.Length; index++)
            {
                itemsControl.Items.Add(splitLines[index]);
            }               
        }      
    }

正如你可以在上面看到,我已经做了步骤1 - 4,遗憾的是,此刻的每一个字进入不同的行。

As you can see above, I already did steps 1 - 4, unfortunately, at the moment each word goes into different row.

下面是我的XAML文件中更大的一部分:

Here is bigger part of my xaml file:

<ItemsControl x:Name="itemsControl"
              ItemsSource="{Binding itemsControl}"
              FontSize="24">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Grid Width="Auto"
                          Margin="0 12"
                                   HorizontalAlignment="Center">
                                <Grid.ColumnDefinitions>                                       
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />                                        
                                </Grid.RowDefinitions>                                    
                                 <StackPanel Grid.Column="0"
                                    Grid.Row="0"
                                    Orientation="Horizontal">
                                    <TextBlock Text="{Binding }" />
                                </StackPanel>
                                <StackPanel Grid.Column="1"
                                    Grid.Row="0"
                                    Orientation="Horizontal">
                                    <TextBlock Text="{Binding }" />
                                </StackPanel>
                            </Grid>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>

我把两个的TextBlocks而不是5现在。如何使每个人得到不同的绑定路径不同的数组元素联系起来?

I put two TextBlocks instead of 5 for now. How to make each of them getting different binding path to link with different array element?

---------- ----------更新
我编辑的TextBlocks所以他们每个人都指向不同的数组元素,像这样:

----------UPDATE---------- I edited TextBlocks so each of them points to different array element like so:

<StackPanel Grid.Column="0"
            Grid.Row="0"
            Orientation="Horizontal">
          <TextBlock Text="{Binding Path=splitLines[0]}" />
</StackPanel>
<StackPanel Grid.Column="1"
            Grid.Row="0"
            Orientation="Horizontal">
          <TextBlock Text="{Binding Path=splitLines[1]}" />
</StackPanel>

我不在这一点上得到任何错误,但遗憾的是没有数据显示在所有。为什么结合数组元素不工作?

I don't get any errors at this point, but unfortunately no data is displayed at all. Why binding to array element is not working?

推荐答案

您可以做到这一点很容易设置 ItemsPanel 的ItemsControl

You can do this quite easily setting ItemsPanel of ItemsControl

修改
新增的ItemTemplate 的ItemsControl

假设你有一个 MainPage.xaml中

<ItemsControl x:Name="WordsList"
              ItemsSource="{Binding listOfWords}">
    <!-- Ensures each item is displayed horizontally -->
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"></StackPanel>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <!--if you want each line in seperate TextBlock with more control-->
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
               <TextBlock Text="{Binding}" FontSize="22" />
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

在后面的 MainPage.xaml.cs中

public sealed partial class MainPage : Page
{
    //Your intial string
    string words = "I am Line 1; I am Line 2; I am Line 3";

    //Using an ObservableCollection as works well with data binding
    public ObservableCollection<string> listOfWords { get; set; } = 
        new ObservableCollection<string>();

    public MainPage()
    {
        this.InitializeComponent();

        //set the DataContext to this page
        this.DataContext = this;

        Loaded += (s, args) =>
        {
            var splitWords = words.Split(';');
            foreach(var word in splitWords)
            {
                //each line is added to listOfWords and can then be accessed by the ItemsControl as the ItemsSource
                //is set to listOfWords
                listOfWords.Add(word);
            }                
        };
    }
}

EDIT2
绑定到数组,您将需要一个公共属性如:

EDIT2 to bind to the array you will need a public property eg

public string[] splitLines { get; set; }

这篇关于如何设置不同的绑定到每个数组元素? C#UWP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆