【问题标题】:How to bind ObservableCollection to LongListSelector?如何将 ObservableCollection 绑定到 LongListSelector?
【发布时间】:2014-01-12 16:17:53
【问题描述】:

我在将 ObservableCollection 绑定到 LongListSelector 时遇到问题,我不知道如何解决。我有两个枢轴项目,第二个我想列出最喜欢的银行。当我点击“add_to_favourites”按钮时,两个银行(在 listPicker 第一个数据透视项目中选择)应该添加到 ObservableCollection 并且它们会添加但它们不会显示在 UI 中。

LongListSelector 的 XAML 是:

</phone:PivotItem>
        <!--Pivot item two-->
        <phone:PivotItem  Header="Ulubione" Margin="10,0,10,7">
            <phone:LongListSelector x:Name ="FavouritesLongList" HorizontalAlignment="Left" Height="531" VerticalAlignment="Top" Width="456" Margin="-251,647,0,-647" LayoutMode="List" IsGroupingEnabled="False" SelectionChanged="Lista_Ulubione_SelectionChanged">
                <phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <TextBlock FontSize="35">
                            <Run Text="{Binding name1}"></Run>
                            <Run Text=" >> "></Run>
                            <Run Text="{Binding name2}"></Run>
                        </TextBlock>
                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
            </phone:LongListSelector>
            <!--Double line list no text wrapping-->
        </phone:PivotItem>
    </phone:Pivot>

现在是我的 ObservableCollection 的样子:

public partial class MainPage : PhoneApplicationPage
{
    public List<Bank> list = new List<Bank>();
    private ObservableCollection<Ulubione> favourites = new ObservableCollection<Ulubione>();

    Wynik wy = new Wynik();
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        zak.DataContext = wy;//TextBlocks DataContext
        ban1.DataContext = wy;
        ban2.DataContext = wy;
        zle.DataContext = wy;
        czas.DataContext = wy;

        DataContext = App.ViewModel;
        this.Banki1.ItemsSource = list;//Item sources for ListPicker
        this.Banki2.ItemsSource = list;}

然后点击我的 add_to_favourites 按钮:

private void addToFavourites(object sender, EventArgs e)
    {
        int b1 = this.Bank1.SelectedIndex;//ListPicker selected index of Bank1
        int b2 = this.Bank2.SelectedIndex;//ListPicker selected index of Bank2
        String n1 = list[this.Banki1.SelectedIndex].name;//Name od Bank1 from list of banks
        String n2 = list[this.Banki2.SelectedIndex].name;/Name od Bank2 from list of banks
        Ulubione fav = new Ulubione(b1, b2, n1, n2);

        bool ifCanAdd = true;
        foreach (Ulubione itm in favourites)
        {
            if (itm.index1 == b1 && itm.index2 == b2)
            {
                MessageBox.Show("This banks are already in favourites");
                ifCanAdd = false;
            }
        }
        if (ifCanAdd == true)
        {
            favourites.Add(fav);
            MessageBox.Show("Added to favourites");
            FavouritesLongList.ItemsSource = favourites;
        }
    }

请帮助我如何将此 ObservableCollection 绑定到 LongListSelector 并在每次向集合添加内容时更新 UI。

【问题讨论】:

  • Binding中的name1和name2是什么?这些是乌鲁比奥内的特性吗?你能告诉乌鲁比奥内吗?为什么每次添加项目时都设置 FavouritesLonglist.ItemsSource?将它移到 this.Banki2.ItemsSource = list; 后面- 不需要每次都设置它,因为它是 ObservableCollection
  • 是的 name1 和 name 2 是 Ulubione 的比例。乌鲁比翁长这样:public class Ulubione { public int index1 { get; set; } public int index2 { get; set; } public String name1 { get; set; } public String name2 { get; set; } public Ulubione(int i1, int i2, String n1, String n2) { index1 = i1; index2 = i2; name1 = n1; name2 = n2; } }
  • 这个 Margin="-251,647,0,-647" 好吗?如果在 InitializeComponent() 之后添加一些东西到收藏夹,你能看到一些东西吗?
  • 不,这不好...这是个问题。谢谢!

标签: c# xaml windows-phone-8 binding longlistselector


【解决方案1】:

问题在于您在 LLS 中设置的边距 - Margin="-251,647,0,-647"。这就是为什么你可能什么都看不到的原因。最美好的祝愿和愉快的编码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 2014-08-23
    • 1970-01-01
    相关资源
    最近更新 更多