Reputation: 55
In xamarin forms development , adding items to listbox doesn't work well.
MainPage.xaml
<ListBox x:Name="vrlist_panel">
<ListBox.ItemContainerStyle>
....
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"></ColumnDefinition>
<ColumnDefinition Width="1*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*"></RowDefinition>
</Grid.RowDefinitions>
<Border Grid.Column="0" Grid.Row="0">
<Image Source="{Binding leftImg}" Height="100" Margin="14,2,13,20"></Image>
</Border>
<Border Grid.Column="1" Grid.Row="0">
<Image Source="{Binding rightImg}" Height="100" Margin="14,2,13,20"></Image>
</Border>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
MainPage.xaml.cs
public class VRDataModel
{
public string leftImg { get; set; }
public string rightImg { get; set; }
}
public partitial class MainPage : Page ,
Autodesk.Revit.UI.IDockablePaneProvider
{
....
private static ObservableCollection<VRDataModel> vrImgs = new
ObservableCollection<VRDataModel>();
....
}
...
vrlist_panel.ItemsSource = vrImgs;
...
Here is entire code.
But even though vrImgs is valid value, the value of vrlist_panel.ItemsSource
is null
on runtime and it causes exception.
I have been trying to fix this issue for several days but couldn't find the reason.
How should I do?
Upvotes: 1
Views: 330
Reputation: 169200
Your example is incomplete but if you are getting a NullReferenceException
when trying to set the ItemsSource
property, you are probably not calling the InitializeComponent()
method prior to setting the property:
public partial class MainPage : Page
{
public MainPage()
{
InitializeComponent();
...
vrlist_panel.ItemsSource = vrImgs;
}
}
Upvotes: 2