如何从一个列表框中的2个来源获取项目

时间:2014-06-04 09:18:16

标签: xml windows-phone-8

在我的wp8应用程序中,我有列表框,在我的列表框中,我有3个按钮和一个文本。按钮从字符串列表中获取标签,我还想将我的Texblock与另一个字符串列表绑定。

但是如何将textblock与其他来源绑定

这是我的列表框

<ListBox Name="URLListBox" Grid.Column="1">
                    <ListBox.ItemTemplate>
                        <DataTemplate>

                            <Grid Background="Transparent" Margin="0,0,0,3">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="200"/>
                                    <ColumnDefinition Width="75"/>
                                    <ColumnDefinition Width="75"></ColumnDefinition>
                                    <ColumnDefinition Width="75"></ColumnDefinition>
                                </Grid.ColumnDefinitions>
                                <TextBlock x:Name="surename" Grid.Column="0" Text="{Binding}" Grid.Column="0" TextWrapping="Wrap" HorizontalAlignment="Left"/>
                                <Button Tag="{Binding}" Grid.Column="1" Content="download" Height="72"  Name="button1"  Width="80" FontSize="15" Click="addButton_Click"/>
                                <Button Tag="{Binding}" Grid.Column="2" Content="play" Height="72"  Name="play_Click"  Width="80" FontSize="15" Click="play_Click"/>
                                <Button Tag="{Binding}" Grid.Column="3" Content="pause" Height="72"  Name="pause_Click"  Width="80" FontSize="15" Click="pause_Click"/>
                            </Grid>


                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

另一个问题是当你向下滚动时,但当你离开屏幕时,它会上升到原来的位置

2 个答案:

答案 0 :(得分:0)

使用Concat组合您的列表。它将创建一个IEnumerable列表

XAML 中,我正在创建列表框;

<ListBox x:Name="listbox"/>

在代码隐藏中, C#

public MainPage()
{
    InitializeComponent();

    var ListOne = new List<string> { "A", "B", "C" };
    var ListTwo = new List<string> { "D", "E", "F" };

    var Combined = ListOne.Concat(ListTwo);

    listbox.ItemsSource = Combined;
 }

答案 1 :(得分:0)

试试这个:

XAML:

    <ListBox Name="URLListBox" Grid.Column="1">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Background="Transparent" Margin="0,0,0,3">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="200"/>
                        <ColumnDefinition Width="75"/>
                        <ColumnDefinition Width="75"></ColumnDefinition>
                        <ColumnDefinition Width="75"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <TextBlock x:Name="surename" Grid.Column="0" Text="{Binding text}" TextWrapping="Wrap" HorizontalAlignment="Left"/>
                    <Button Tag="{Binding b1Tag}" Grid.Column="1" Content="download" Height="72"  Name="button1"  Width="80" FontSize="15" Click="addButton_Click"/>
                    <Button Tag="{Binding b1Tag}" Grid.Column="2" Content="play" Height="72"  Name="play_Click"  Width="80" FontSize="15" Click="play_Click"/>
                    <Button Tag="{Binding b1Tag}" Grid.Column="3" Content="pause" Height="72"  Name="pause_Click"  Width="80" FontSize="15" Click="pause_Click"/>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

CS:

public void LoadData()
{
        List<string> TextData = new List<string> { "Text1", "Text2", "Text3", "Text4" };
        List<string> BtnData = new List<string> { "B1Tag", "B2Tag", "B3Tag", "B4Tag" };
        ObservableCollection<URLListBoxCalss> obj = new ObservableCollection<URLListBoxCalss>();
        for (int i = 0; i < 4; i++)
        {
            obj.Add(new URLListBoxCalss(TextData[i], BtnData[i], BtnData[i], BtnData[i]));
        }
        URLListBox.ItemsSource = obj;
    }


public class URLListBoxCalss
{
    public string text { get; set; }
    public string b1Tag { get; set; }
    public string b2Tag { get; set; }
    public string b3Tag { get; set; }

    public URLListBoxCalss() { }

    public URLListBoxCalss(string text, string b1Tag, string b2Tag, string b3Tag)
    {
        this.text = text;
        this.b1Tag = b1Tag;
        this.b2Tag = b2Tag;
        this.b3Tag = b3Tag;
    }   
}