When I adding Genre of book by Listpicker, it's successful but when I choose the Book title in Listbox in order to show the details of Book, the value of Genre does not pass. What I want is in detail page, Genre is showed in Listpicker (as in Add page) and show the Genre I had chosen before
Addpage.xaml
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Name="listpickertemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Genre}" Margin="10 0 0 0" />
</StackPanel>
</DataTemplate>
<Grid x:Name="ContentPanel1" Grid.Row="1" >
<ListBox x:Name="BookInfo" >
<TextBlock Text="Book Title: *" FontWeight="Normal" FontStyle="Normal" Style="{StaticResource PhoneTextTitle3Style}" />
<TextBox x:Name="booktitletext" Width="460" TextWrapping="Wrap"/>
<TextBlock Text="Genre: *" FontWeight="Normal" FontStyle="Normal" Style="{StaticResource PhoneTextTitle3Style}" />
<toolkit:ListPicker x:Name="ListPicker" ItemTemplate="{StaticResource listpickertemplate}" Width="120" HorizontalAlignment="Left"/>
</ListBox>
</Grid>
The code behind Addpage:
public AddingPage()
{
InitializeComponent();
this.DataContext = App.MainViewModel;
List<GenrePicker> newpicker = new List<GenrePicker>();
newpicker.Add(new GenrePicker() { Genre = "Comedy",Index = 0 });
newpicker.Add(new GenrePicker() { Genre = "Science",Index = 1 });
newpicker.Add(new GenrePicker() { Genre = "Action", Index = 2 });
this.ListPicker.ItemsSource = newpicker;
}
private void Add_Click(object sender, EventArgs e)
{
if (booktitletext.Text.Length > 0)
{
Book newbook = new Book
{
BookTitle = booktitletext.Text,
Genre = (ListPicker.SelectedItem as GenrePicker).Genre.ToString(),
}
App.MainViewModel.Addinfo(newbook);
}
}
In my Browsepage.xaml, I just show the title of the book
<Grid x:Name="ContentPanel1" Margin="12,0,12,0">
<ListBox x:Name="TitleList" SelectionChanged="TitleList_SelectionChanged" ItemsSource="{Binding Load0}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Width="466" Margin="0, 0, 0, 12">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="360"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0"></Grid>
<StackPanel Grid.Column="1">
<TextBlock FontSize="40" Text="{Binding BookTitle}" FontWeight="Normal" FontStyle="Normal" Style="{StaticResource PhoneTextTitle3Style}" TextWrapping="Wrap"/>
</StackPanel>
<Grid Grid.Column="2">
<Button x:Name="Deletebutton" Height="50" Width="50" Click="deleteButton_Click" BorderBrush="{StaticResource TransparentBrush}" Margin="-40">
<Image Source="/Assets/delete.dark.png" Height="50" Width="50" Visibility="{StaticResource PhoneDarkThemeVisibility}" Margin="-40" />
</Button>
</Grid>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
The code behind of Browsepage when i choose a book title:
private void TitleList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// If no Book is selected, just return
if (TitleList.SelectedIndex== -1) return;
// Get the parent application that contains the Book being edited
App thisApp = Application.Current as App;
// Set this to the selected customer
thisApp.SelectedBook = TitleList.SelectedItem as Book;
thisApp.SelectedGenre = TitleList.SelectedItem as GenrePicker;
// Navigate to the detail page
NavigationService.Navigate(new Uri("/View/DetailPage/BookDetail.xaml",UriKind.RelativeOrAbsolute));
TitleList.SelectedIndex = -1;
}
And the Bookdetails.xaml:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" >
<ListBox x:Name="BookDetails">
<TextBlock x:Name="booktitle" Text="Book Title:" Foreground="#FF3B12AA" FontSize="30" FontWeight="Bold" FontFamily="Courier New"/>
<TextBox x:Name="booktitletext" Text="{Binding BookTitle, Mode=TwoWay}" Width="460" TextWrapping="Wrap" Background="#BF22A1DC" BorderBrush="#BFFFFFFF" FontFamily="Tahoma" FontSize="26"/>
<TextBlock x:Name="author" Text="Author:" Foreground="#FF3B12AA" FontSize="30" FontWeight="Bold" FontFamily="Courier New"/>
<TextBox x:Name="authortext" Tap="authortext_Tap" Text="{Binding Author, Mode=TwoWay}" Width="460" TextWrapping="Wrap" Background="#BF22A1DC" BorderBrush="#BFFFFFFF" FontFamily="Tahoma" FontSize="26"/>
<TextBlock x:Name="genre" Text="Genre:" Foreground="#FF3B12AA" FontSize="30" FontWeight="Bold" FontFamily="Courier New"/>
<toolkit:ListPicker x:Name="ListPicker" ItemTemplate="{StaticResource listpickertemplate}" Width="120" HorizontalAlignment="Left" Background="#BF3BDC22" SelectedIndex="{Binding Index}"/>
</ListBox>
</Grid>
the code-behind Bookdetails page:
public BookDetail()
{
InitializeComponent();
this.DataContext = App.MainViewModel;
List<GenrePicker> newpicker = new List<GenrePicker>();
newpicker.Add(new GenrePicker() { Genre = "Comedy",Index = 0});
newpicker.Add(new GenrePicker() { Genre = "Science", Index = 1 });
newpicker.Add(new GenrePicker() { Genre = "Action", Index = 2});
this.ListPicker.ItemsSource = newpicker;
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
// Get the parent application
App thisApp = Application.Current as App;
// Load the active customer into the viewmodel
App.MainViewModel.LoadDetails(thisApp.SelectedBook,thisApp.SelectedGenre);
}
and this is the Loadtails function
public void LoadDetails(Book bcd,GenrePicker xyz)
{
Index = xyz.Index;
BookTitle = bcd.BookTitle;
Genre = bcd.Genre;
}
I have try emulate it but it show error when I choose the booktile System.InvalidOperationException: SelectedIndex must always be set to a valid value.
at this.DataContext = App.MainViewModelof Bookdetails Page
My guess, the problem is your code setting DataContext (which means setting ListPicker's selected index too) before populating ListPicker's ItemsSource. That will cause selecting a non-existent -yet- Item of the ListPicker. Try to reorder codes to populate ListPicker before setting DataContext :
public BookDetail()
{
InitializeComponent();
List<GenrePicker> newpicker = new List<GenrePicker>();
newpicker.Add(new GenrePicker() { Genre = "Comedy",Index = 0});
newpicker.Add(new GenrePicker() { Genre = "Science", Index = 1 });
newpicker.Add(new GenrePicker() { Genre = "Action", Index = 2});
this.ListPicker.ItemsSource = newpicker;
this.DataContext = App.MainViewModel;
}
Instead of SelectedIndex = -1, try SelectedItem = null, which makes more sense.
When you set SelectedIndex = -1, you tell the ListPicker, "Hey, go find the -1'th element in your list and set that as selected." Then your ListPicker crashes because it doesn't understand what ListPicker[-1] means.
Related
I am trying to update a data-binded list view.
This is the desired behaviour:
The user writes the title of the item he/she likes to add to the list and submits his input with the enter key.
The list should update, but it doesn't.
private void NewSubject_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
TextBox newSubject = sender as TextBox;
Subjects.Add(new Subject { Title = newSubject.Text });
SubjectsList.ItemsSource = Subjects;
newSubject.Text = "";
}
}
this is the xaml code excerpt:
<DockPanel Margin="4">
<TextBox x:Name="NewSubject" KeyDown="NewSubject_KeyDown" DockPanel.Dock="Bottom" Margin="0 8 0 0" Padding="4" />
<ListView Name="SubjectsList" ItemsSource="{Binding Subjects}" DockPanel.Dock="Bottom">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Title}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</DockPanel>
Where could be my mistake?
I have a gridview with data bindings that contain titles and book covers. Cover the book is in the local package with the name of the path: "files/kurikulum 2006/[The name of the folder selected by the user on the previous page]/cover".I have trouble displaying the cover of the book so that the book cover does not appear and the error message appears as below:
But for the title can be displayed on the gridview.
XAML:
<GridView
x:Name="itemGridView"
AutomationProperties.AutomationId="ItemGridView"
AutomationProperties.Name="Grouped Items"
Margin="0,0,10,0"
HorizontalAlignment="Center"
ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"
SelectionMode="None"
IsSwipeEnabled="false"
IsItemClickEnabled="True"
ItemClick="ItemView_ItemClick" BorderThickness="0">
<GridView.ItemTemplate>
<DataTemplate>
<Grid Height="315" Width="200" Margin="5,10,0,0" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Grid.Row="0" Margin="10,10,10,10" Height="230" Width="180" Source="{Binding Image}" AutomationProperties.Name="{Binding Name}" />
<Grid Grid.Row="1" Margin="0,0,10,10" HorizontalAlignment="Left" Height="50" >
<ScrollViewer Margin="10,10,5,5" VerticalAlignment="Top" HorizontalAlignment="Left" Height="40" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled">
<TextBlock Margin="0,0,10,10" Text="{Binding Name}" Foreground="Black" FontSize="25" VerticalAlignment="Center" HorizontalAlignment="Left" FontFamily="Segoe UI Black" FontWeight="SemiBold" TextAlignment="Center" TextWrapping="Wrap" Height="40" Width="auto"/>
</ScrollViewer>
</Grid>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
Code:
ObservableCollection<Book> datasource = new ObservableCollection<Book>();
StorageFolder _folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
_folder = await _folder.GetFolderAsync("files");
_folder = await _folder.GetFolderAsync(direktori.Direktori);
_folder = await _folder.GetFolderAsync(direktori.Name);
_folder = await _folder.GetFolderAsync("cover");
IReadOnlyList<StorageFile> _files = await _folder.GetFilesAsync(); //which returns List<StorageFile>
foreach (StorageFile file in _files)
{
Book buku = new Book();
buku.Name = file.DisplayName.ToString();
StorageFile thumbFile;
thumbFile = await _folder.GetFileAsync(file.DisplayName.ToString() + ".jpg");
BitmapImage bi = new BitmapImage();
bi.SetSource(await thumbFile.OpenAsync(FileAccessMode.Read));
buku.Image = bi;
datasource.Add(buku);
}
itemGridView.ItemsSource = datasource;
Book class:
public class Book
{
public string Name { get; set; }
public string Direktori { get; set; }
public ImageSource Image { get; set; }
}
How to handle it?
Note:
direktori.Direktori = kurikulum 2006 (or the other folder)
direktori.Name = The name of the folder selected by the user on the previous page
This is my working code,
private ObservableCollection<User> _Users;
public ObservableCollection<User> Users
{
get { return _Users; }
set { _Users = value; RaisePropertyChanged(()=> Users); }
}
Users = new ObservableCollection<User>();
for (int i = 1; i <= 10; i++)
{
Users.Add(new User()
{
ADDRESS_LINE_1 = "Test Address",
ADDRESS_LINE_2 = "Test Address 2",
FIRST_NAME = "Test Name " + i,
SURNAME = "Test surname " +i,
DATE_OF_BIRTH = DateTime.Now.Date,
GENDER = "M",
MOBILE_PHONE_NUMBER = "+1100000",
EMAIL_ADDRESS = "Test#email.com",
LAST_MODIFIED = DateTime.Now,
LOGIN_NAME ="operator.domain.com",
ItemIndex = Users.Count +1
});
}
earlier i am using
private List<User> _Users;
public List<User> Users
{
get { return _Users; }
set { _Users = value; RaisePropertyChanged(()=> Users); }
}
Users = new List<User>();
for (int i = 1; i <= 10; i++)
{
Users.Add(new User()
{
ADDRESS_LINE_1 = "Test Address",
ADDRESS_LINE_2 = "Test Address 2",
FIRST_NAME = "Test Name " + i,
SURNAME = "Test surname " +i,
DATE_OF_BIRTH = DateTime.Now.Date,
GENDER = "M",
MOBILE_PHONE_NUMBER = "+9100000",
EMAIL_ADDRESS = "Test#email.com",
LAST_MODIFIED = DateTime.Now,
LOGIN_NAME ="operator1.domain.com",
ItemIndex = Users.Count +1
});
}
and i am continuously getting following exception message, when i try to bind it with datatemplate in xaml
Value does not fall within the expected range.
Stack trace is null :(
In addition, if i take another list variable and add those values to it and then assign shallow copy of that list to the my above list, the code give me desired result, but again what actually going around.
This is the datatemplate
<DataTemplate>
<Grid Margin="-8,-10,-8,-10" Background="{Binding ItemIndex, Converter={StaticResource AlternateRowBackgroundConverter}}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" Style="{StaticResource BorderStyleForAdmin}" Background="{Binding ItemIndex, Converter={StaticResource AlternateRowBackgroundConverter}}">
<StackPanel Orientation="Horizontal">
<!--<Border BorderBrush="Gray" BorderThickness="0.3" Visibility="{Binding IsSelectedItem, Mode=TwoWay, Converter={StaticResource ControlVisibiltyOfSelectedItemInListBox}}">
<TextBlock Text="{StaticResource ArrowGlyph}" Foreground="Black" VerticalAlignment="Center" HorizontalAlignment="Right"></TextBlock>
</Border>-->
<TextBlock Style="{StaticResource TextBlockCell}" Text="{Binding SURNAME}"></TextBlock>
</StackPanel>
</Border>
<Border Grid.Column="1" Style="{StaticResource BorderStyleForAdmin}" Background="{Binding ItemIndex, Converter={StaticResource AlternateRowBackgroundConverter}}">
<TextBlock Style="{StaticResource TextBlockCell}" Text="{Binding FIRST_NAME}"></TextBlock>
</Border>
<Border Grid.Column="2" Style="{StaticResource BorderStyleForAdmin}" Background="{Binding ItemIndex, Converter={StaticResource AlternateRowBackgroundConverter}}">
<TextBlock Style="{StaticResource TextBlockCell}" HorizontalAlignment="Stretch">
<Run Text="{Binding ADDRESS_LINE_1}"></Run><LineBreak></LineBreak>
<Run Text="{Binding ADDRESS_LINE_2}"></Run>
</TextBlock>
</Border>
<Border Grid.Column="3" Style="{StaticResource BorderStyleForAdmin}" Background="{Binding ItemIndex, Converter={StaticResource AlternateRowBackgroundConverter}}">
<TextBlock Style="{StaticResource TextBlockCell}" Text="{Binding DATE_OF_BIRTH}"></TextBlock>
</Border>
<Border Grid.Column="4" Style="{StaticResource BorderStyleForAdmin}" Background="{Binding ItemIndex, Converter={StaticResource AlternateRowBackgroundConverter}}">
<TextBlock Style="{StaticResource TextBlockCell}" Text="{Binding EMAIL_ADDRESS}"></TextBlock>
</Border>
<Border Grid.Column="5" Style="{StaticResource BorderStyleForAdmin}" Background="{Binding ItemIndex, Converter={StaticResource AlternateRowBackgroundConverter}}">
<TextBlock Style="{StaticResource TextBlockCell}" Text="{Binding MOBILE_PHONE_NUMBER}"></TextBlock>
</Border>
</Grid>
</DataTemplate>
Though, Observable collection works for me but i am wondering why List giving me this exception, i am unable to figure out what's the actual reason behind the scene. Thanks in advance.
Only thing which bugs me out here is "RaisePropertyChanged" is used with ObservableCollection or at least you must inherit INotifyPropertyChanged interface. In your second implementation list is not getting "RaisePropertyChanged" method. Consider list declaration as:
private List<User> _Users;
public List<User> Users
{
get { return _Users; }
set { _Users = value;}
}
I think this should resolve the error. But NOTE that doing so will not raise PropertyChanged event whenever the state of the object changes (Added, Removed, and Modified) to the point where you want to notify the underlying collection or container that the state has changed.
For more info read: List vs ObservableCollection vs INotifyPropertyChanged in Silverlight
I'm using this RSS feed for my Windows 8 Application (c#) http://www.skysports.com/rss/0,20514,11661,00. I can display the Title and PubDate but I'm stuck on the image.
private async void LoadRSS()
{
SyndicationClient client = new SyndicationClient();
Uri feedUri = new Uri("http://www.skysports.com/rss/0,20514,11661,00.xml");
SyndicationFeed feed = await client.RetrieveFeedAsync(feedUri);
FeedData feedData = new FeedData();
foreach (SyndicationItem item in feed.Items)
{
FeedItem feedItem = new FeedItem();
feedItem.Title = item.Title.Text;
feedItem.PubDate = item.PublishedDate.DateTime;
// Handle the differences between RSS and Atom feeds.
if (feed.SourceFormat == SyndicationFormat.Atom10)
{
feedItem.Content = item.Content.Text;
feedItem.Link = new Uri("http://www.skysports.com" + item.Id);
}
else if (feed.SourceFormat == SyndicationFormat.Rss20)
{
feedItem.Content = item.Summary.Text;
feedItem.Link = item.Links[0].Uri;
}
feedData.Items.Add(feedItem);
}
ItemListView.DataContext = feedData.Items;
}
xaml code:
<GridView x:Name="ItemListView" Grid.Column="2" Grid.Row="1" ItemsSource="{Binding}" ItemClick="Sports_ItemClick_1" IsItemClickEnabled="True" SelectionMode="None" >
<GridView.ItemTemplate>
<DataTemplate>
<Grid Width="400" Height="75" Margin="0,0,50,0" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="75"></ColumnDefinition>
<ColumnDefinition Width="325"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Border Background="#60BF89">
<Image Source="{Binding ImagePath, Mode=OneWay}" Stretch="None" Margin="5,15,0,0" VerticalAlignment="Top" HorizontalAlignment="Center" Width="500"/>
</Border>
<StackPanel Grid.Column="1" VerticalAlignment="Bottom" Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}">
<TextBlock Foreground="White" Text="{Binding Title}" FontSize="16" Margin="5,0,0,0" TextWrapping="Wrap" />
<TextBlock Foreground="White" Text="{Binding PubDate}" FontSize="12" Margin="5,0,0,0"/>
</StackPanel>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
I would like the Image in the first column. Any ideas??
you are not setting ImagePath property in the code sample you showed, considering this as a problem
I am working on set of radiobuttons which are dynamicaly generated in my app. I seem to be facing a issue when it comes to access the class members. Here is what I have done till now:
XAML:
<GroupBox Header="Daughter Cards" Height="Auto" HorizontalAlignment="Stretch" Margin="20,5,20,20" Name="groupBox2" VerticalAlignment="Stretch" Width="Auto">
<Grid>
<Grid Grid.Column="0">
<ItemsControl ItemsSource="{Binding SlotChildren}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="3" Rows="8" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton Content="{Binding SlotButtons}" Margin="0,10,0,0" IsChecked="{Binding IsChecked}" GroupName="SlotGroup" Height="15" Width="80" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
<Grid Grid.Column="1">
<ComboBox Grid.Row="0" ItemsSource="{Binding DaughterBoardBoxList}" SelectedItem="{Binding SelectedDaughterBoardBoxList, Mode=TwoWay}" SelectedIndex="0" Height="23" HorizontalAlignment="Center" Margin="0" Name="comboBox5" VerticalAlignment="Center" Width="158" />
<ComboBox Grid.Row="1" ItemsSource="{Binding DaughterVersionBoxList}" SelectedItem="{Binding SelectedDaughterVersionBoxList, Mode=TwoWay}" SelectedIndex="0" Height="23" HorizontalAlignment="Center" Margin="0" Name="comboBox6" VerticalAlignment="Center" Width="158" />
<ComboBox Grid.Row="2" ItemsSource="{Binding DaughterSerialBoxList}" SelectedItem="{Binding SelectedDaughterSerialBoxList, Mode=TwoWay}" SelectedIndex="0" Height="23" HorizontalAlignment="Center" Margin="0" Name="comboBox7" VerticalAlignment="Center" Width="158" />
<Button Grid.Row="1" Command="{Binding GetStringCommand}" Content="Get String" Height="23" HorizontalAlignment="Center" Margin="0" Name="RefreshDaughterCards" VerticalAlignment="Center" Width="90" />
<Button Grid.Row="2" Command="{Binding SetStringCommand}" Content="Set String" Height="23" HorizontalAlignment="Center" Margin="0" Name="WriteEEPROMDCBtn" VerticalAlignment="Center" Width="90" />
<Label Content="{Binding DaughterStatus}" Height="25" HorizontalAlignment="Center" Margin="0" Name="DaughterCardLabel" VerticalAlignment="Center" Width="170" />
</Grid>
</Grid>
</GroupBox>
EEPROMViewModel Class:
public ObservableCollection<EEPROMSlotViewModel> SlotChildren { get; set; }
public EEPROMViewModel ()
{
SlotChildren = new ObservableCollection<EEPROMSlotViewModel>();
SlotChildren.Add(new EEPROMSlotViewModel() { ParentVM = this, SlotButtons = "0 : None", ID = 0 });
SlotChildren.Add(new EEPROMSlotViewModel() { ParentVM = this, SlotButtons = "1 : None", ID = 1 });
SlotChildren.Add(new EEPROMSlotViewModel() { ParentVM = this, SlotButtons = "2 : None", ID = 2 });
SlotChildren.Add(new EEPROMSlotViewModel() { ParentVM = this, SlotButtons = "3 : None", ID = 3 });
SlotChildren.Add(new EEPROMSlotViewModel() { ParentVM = this, SlotButtons = "4 : None", ID = 4 });
SlotChildren.Add(new EEPROMSlotViewModel() { ParentVM = this, SlotButtons = "5 : None", ID = 5 });
SlotChildren.Add(new EEPROMSlotViewModel() { ParentVM = this, SlotButtons = "6 : None", ID = 6 });
}
generates 7 radiobuttons with ID related to each.
EEPROMSlotViewModel Class:
private string _SlotButtons;
public string SlotButtons
{
get; set;
}
private EEPROMViewModel _parentVm;
public EEPROMViewModel ParentVM
{
get; set;
}
private int _ID;
public int ID
{
get; set;
}
private bool _isChecked;
public bool IsChecked
{
get; set;
}
thus whenever I select a radiobutton and click SETSTRING button, the following code gets executed:
EEPROMSlotViewModel mSlotVM = new EEPROMSlotViewModel();
string label;
if (mSlotVM.ID == 0) //Accessing the 1st radiobutton clicked
{
label = string.Empty;
mSlotVM.getShortName(0, label);
if (label == string.Empty)
{
label = "None";
}
mSlotVM.SlotButtons = Convert.ToString(0 + ":" + label); // Setting CONTENT of radiobutton selected
}
Lets say I clicked 1st radio button, ID is supposed to be 0. It calls getShortName() method which does the following:
ParentVM.SelectedDaughterBoardBoxList = ParentVM.DaughterBoardBoxList[0];
ParentVM.SelectedDaughterVersionBoxList = ParentVM.DaughterVersionBoxList[0];
ParentVM.SelectedDaughterSerialBoxList = ParentVM.DaughterSerialBoxList[0];
shortlabel = "Hello";
I am facing few issues here:
Is mSlotVM right way to access other class members/function??
Once control enters getShortname(), it throws the excetion as follows: Object reference not set to an instance of an object. at
ParentVM.DaughterBoardBoxList[0];.
Even if I comment the first 3 statements in getShortName(), When getShortName gets called and once the control comes back, value of
label is "", i should be "hello".
I feel mSlotVm this is the reason behind the exception. Please help :)
No, you just create a new instance of the EEPROMSlotViewModel-Class, youre not accessing any of the RadioButtons ViewModels.
It would be helpful if you could show us your EEPROMViewModel-Class. I think the problem is, that your ParentVM.-Lists are null.
To achieve what you want, your getShortname()-method has to look like:
public void getShortname(int i, ref string shortlabel)
{
ParentVM.SelectedDaughterBoardBoxList = ParentVM.DaughterBoardBoxList[0];
ParentVM.SelectedDaughterVersionBoxList = ParentVM.DaughterVersionBoxList[0];
ParentVM.SelectedDaughterSerialBoxList = ParentVM.DaughterSerialBoxList[0];
shortlabel = "Hello";
}
EDIT:
Through EEPROMSlotViewModel mSlotVM = new EEPROMSlotViewModel(); you create a new instance of EEPROMSlotViewModel, but youre not getting the checked RadioButtons ViewModel. So at the point were you call mSlotVM.getShortName(0, label); mSlotVM has no ParentVM, thats what raises the exception. What you could do is go through your SlotChildren-List and take the EEPROMSlotViewModel whose IsChecked-property is true.
Example:
EEPROMSlotViewModel checkedVM;
string label = string.Empty;
foreach (EEPROMSlotViewModel vm in SlotChildren)
{
if (vm.IsChecked)
{
checkedVM = vm;
}
else
{
vm.SlotButtons = vm.ID + " : NONE"
}
}
checkedVM.getShortName(0, ref label);
if (label == string.Empty)
{
label = "None";
}
checkedVM.SlotButtons = Convert.ToString(0 + ":" + label); // Setting CONTENT of radiobutton selected