我有一个将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。
答案 0 :(得分:1)
问题在于您在LLS中设置的保证金 - 保证金=“ - 251,647,0,-647”。这就是为什么你可能看不到任何东西。祝愿和编码愉快。