如何为每个Array元素设置不同的Binding? C#UWP

时间:2016-06-09 10:36:50

标签: c# xaml binding split uwp

我设法在TextBlock中显示我的数组 - 每个[element]在单独的行中,但我想将每个Array [index]元素放在同一行中的单独TextBlock中,只要它来自同一行,所以:

  1. 访问文件
  2. 行已排序
  3. 行分为单词
  4. 每一行都放在单独的行上
  5. 5。来自行的每个单词都放在单独的TextBlock中但在同一行中。

    这是我的代码:

    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,此时每个单词都会进入不同的行。

    这是我的xaml文件的更大部分:

    <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。如何让每个人获得不同的绑定路径来链接不同的数组元素?

    ---------- ---------- UPDATE 我编辑了TextBlocks,所以每个都指向不同的数组元素,如下所示:

    <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>
    

    此时我没有收到任何错误,但遗憾的是根本没有显示任何数据。为什么绑定到数组元素不起作用?

1 个答案:

答案 0 :(得分:0)

您可以轻松设置ItemsPanel

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 要绑定到数组,您将需要一个公共属性,例如

public string[] splitLines { get; set; }