C
CrazyVibe
Guest
Hi,
I have the following class:
public class Multimedia : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public enum MediaType
{
CD,
DVD,
BRay,
Steraming,
};
private string _title;
private string _artist;
private string _genre;
private MediaType _type;
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public string Title
{
get { return _title; }
set
{
_title = value;
this.NotifyPropertyChanged("title");
}
}
public string Artist
{
get { return _artist; }
set
{
_artist = value;
this.NotifyPropertyChanged("Artist");
}
}
public string Genre
{
get { return _genre; }
set
{
_genre = value;
this.NotifyPropertyChanged("Genre");
}
}
public MediaType Type
{
get { return _type; }
set
{
_type = value;
this.NotifyPropertyChanged("Type");
}
}
private ObservableCollection<Multimedia> listItems;
public ObservableCollection<Multimedia> ListItems
{
get { return listItems; }
set
{
listItems = value;
this.NotifyPropertyChanged("Multimedia List");
}
}
public override string ToString()
{
return String.Format(CultureInfo.CurrentCulture,"Multimedia Items: {0}{1}{2}{3} :",this.Title,this.Artist, this.Genre,this.Type);
}
}
In my MainWindow.xaml I have the following:
<Window x:Class="Assignment5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Assignment5"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="461">
<Window.DataContext>
<local:Multimedia/>
</Window.DataContext>
<Grid Margin="10,0,-8,0">
<Grid.RowDefinitions>
<RowDefinition Height="0*"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0*"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ListBox Name="MediaList" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding ListItems}" DisplayMemberPath="Title" HorizontalAlignment="Left" Height="176" Margin="214,1,0,0" VerticalAlignment="Top" Width="237" SelectionChanged="ListBox_SelectionChanged" Grid.Column="1" Grid.Row="1"/>
<TextBox Name="txtBoxList" HorizontalAlignment="Left" Height="176" Margin="0,1,0,0" Text="{Binding ElementName=MediaList,Path=SelectedValue}" TextWrapping="Wrap" VerticalAlignment="Top" Width="214" Grid.Column="1" Grid.Row="1"/>
<Button Grid.ColumnSpan="2" Content="Add multimedia element" HorizontalAlignment="Left" Margin="192,333,0,0" Grid.RowSpan="2" VerticalAlignment="Top" Width="142" Height="42" Click="Button_Click"/>
<ComboBox x:Name="mediaType" Selector.IsSelected="True" HorizontalAlignment="Left" Margin="214,198,0,0" ItemsSource="{Binding Source={StaticResource enmPositions}}" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.175,0.773" SelectionChanged="ComboBox_SelectionChanged" Grid.ColumnSpan="2" Grid.RowSpan="2"/>
<Label Content="Media type" HorizontalAlignment="Left" Margin="15,198,0,0" VerticalAlignment="Top" Width="113" Grid.ColumnSpan="2" Grid.RowSpan="2"/>
<Label Content="Title" HorizontalAlignment="Left" Margin="15,224,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.605,0.115" Width="113" Grid.ColumnSpan="2" Grid.RowSpan="2"/>
<Label Content="Artist" HorizontalAlignment="Left" Margin="15,250,0,0" VerticalAlignment="Top" Width="113" Grid.ColumnSpan="2" Grid.RowSpan="2"/>
<Label Content="Genre" HorizontalAlignment="Left" Margin="15,276,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.553,0.192" Width="113" Grid.ColumnSpan="2" Grid.RowSpan="2"/>
<TextBox x:Name="txtTitle" HorizontalAlignment="Left" Height="22" Margin="214,224,0,0" TextWrapping="Wrap" Text="{Binding Title}" VerticalAlignment="Top" Width="120" TextChanged="TextBox_TextChanged" Grid.ColumnSpan="2" Grid.RowSpan="2"/>
<TextBox x:Name="txtArtist" HorizontalAlignment="Left" Height="22" Margin="214,250,0,0" TextWrapping="Wrap" Text="{Binding Artist}" VerticalAlignment="Top" Width="120" Grid.ColumnSpan="2" Grid.RowSpan="2"/>
<TextBox x:Name="txtGenre" HorizontalAlignment="Left" Height="22" Margin="214,276,0,0" TextWrapping="Wrap" Text="{Binding Genre}" VerticalAlignment="Top" Width="120" Grid.ColumnSpan="2" Grid.RowSpan="2"/>
</Grid>
</Window>
My problem is in t
he code behind the view when I need to add the newly inserted items from the textboxes along with the value from the combobox into the listbox which has "observ" List as Datasource.This is how the items are added to the listbox:
private void Button_Click(object sender, RoutedEventArgs e)
{
MediaList.Items.Add(txtArtist.Text);//this is where the error occurs
MediaList.Items.Add(txtGenre.Text);
MediaList.Items.Add(txtTitle.Text);
MediaList.Items.Add(mediaType.Text);
}
ObservableCollection<Multimedia> observ = new ObservableCollection<Multimedia>();
public MainWindow()
{
InitializeComponent();
MediaList.DataContext =this;
observ.Add(new Multimedia() { Title = "Coco Jambo", Artist = "Madonna", Genre = "Hip-Hop", Type = Multimedia.MediaType.CD });
observ.Add(new Multimedia() { Title = "Mercy", Artist = "Duffy", Genre = "Dance", Type = Multimedia.MediaType.DVD });
observ.Add(new Multimedia() { Title = "Live your life", Artist = "Rihanna", Genre = "Hip-Hop", Type = Multimedia.MediaType.BRay });
observ.Add(new Multimedia() { Title = "Oppss...I did it again", Artist = "Britney Spears", Genre = "Dance", Type = Multimedia.MediaType.CD });
observ.Add(new Multimedia() { Title = "Dead man walking", Artist = "Smiley", Genre = "Hip-Hop", Type = Multimedia.MediaType.DVD });
MediaList.ItemsSource = observ;
m.ListItems = observ;
}
The error that i am getting is related to the ItemsSource:"Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead."I have tried to clear the list before adding the elements like this:Media.Items.Clear() but without any luck.I have also tried to add directly to the list "observ" and clear it but still didn't work by throwing another error"Object not set to an instance of an object".Can anyone help me with this problem?
Best regards,
M
Continue reading...
I have the following class:
public class Multimedia : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public enum MediaType
{
CD,
DVD,
BRay,
Steraming,
};
private string _title;
private string _artist;
private string _genre;
private MediaType _type;
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public string Title
{
get { return _title; }
set
{
_title = value;
this.NotifyPropertyChanged("title");
}
}
public string Artist
{
get { return _artist; }
set
{
_artist = value;
this.NotifyPropertyChanged("Artist");
}
}
public string Genre
{
get { return _genre; }
set
{
_genre = value;
this.NotifyPropertyChanged("Genre");
}
}
public MediaType Type
{
get { return _type; }
set
{
_type = value;
this.NotifyPropertyChanged("Type");
}
}
private ObservableCollection<Multimedia> listItems;
public ObservableCollection<Multimedia> ListItems
{
get { return listItems; }
set
{
listItems = value;
this.NotifyPropertyChanged("Multimedia List");
}
}
public override string ToString()
{
return String.Format(CultureInfo.CurrentCulture,"Multimedia Items: {0}{1}{2}{3} :",this.Title,this.Artist, this.Genre,this.Type);
}
}
In my MainWindow.xaml I have the following:
<Window x:Class="Assignment5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Assignment5"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="461">
<Window.DataContext>
<local:Multimedia/>
</Window.DataContext>
<Grid Margin="10,0,-8,0">
<Grid.RowDefinitions>
<RowDefinition Height="0*"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0*"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ListBox Name="MediaList" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding ListItems}" DisplayMemberPath="Title" HorizontalAlignment="Left" Height="176" Margin="214,1,0,0" VerticalAlignment="Top" Width="237" SelectionChanged="ListBox_SelectionChanged" Grid.Column="1" Grid.Row="1"/>
<TextBox Name="txtBoxList" HorizontalAlignment="Left" Height="176" Margin="0,1,0,0" Text="{Binding ElementName=MediaList,Path=SelectedValue}" TextWrapping="Wrap" VerticalAlignment="Top" Width="214" Grid.Column="1" Grid.Row="1"/>
<Button Grid.ColumnSpan="2" Content="Add multimedia element" HorizontalAlignment="Left" Margin="192,333,0,0" Grid.RowSpan="2" VerticalAlignment="Top" Width="142" Height="42" Click="Button_Click"/>
<ComboBox x:Name="mediaType" Selector.IsSelected="True" HorizontalAlignment="Left" Margin="214,198,0,0" ItemsSource="{Binding Source={StaticResource enmPositions}}" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.175,0.773" SelectionChanged="ComboBox_SelectionChanged" Grid.ColumnSpan="2" Grid.RowSpan="2"/>
<Label Content="Media type" HorizontalAlignment="Left" Margin="15,198,0,0" VerticalAlignment="Top" Width="113" Grid.ColumnSpan="2" Grid.RowSpan="2"/>
<Label Content="Title" HorizontalAlignment="Left" Margin="15,224,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.605,0.115" Width="113" Grid.ColumnSpan="2" Grid.RowSpan="2"/>
<Label Content="Artist" HorizontalAlignment="Left" Margin="15,250,0,0" VerticalAlignment="Top" Width="113" Grid.ColumnSpan="2" Grid.RowSpan="2"/>
<Label Content="Genre" HorizontalAlignment="Left" Margin="15,276,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.553,0.192" Width="113" Grid.ColumnSpan="2" Grid.RowSpan="2"/>
<TextBox x:Name="txtTitle" HorizontalAlignment="Left" Height="22" Margin="214,224,0,0" TextWrapping="Wrap" Text="{Binding Title}" VerticalAlignment="Top" Width="120" TextChanged="TextBox_TextChanged" Grid.ColumnSpan="2" Grid.RowSpan="2"/>
<TextBox x:Name="txtArtist" HorizontalAlignment="Left" Height="22" Margin="214,250,0,0" TextWrapping="Wrap" Text="{Binding Artist}" VerticalAlignment="Top" Width="120" Grid.ColumnSpan="2" Grid.RowSpan="2"/>
<TextBox x:Name="txtGenre" HorizontalAlignment="Left" Height="22" Margin="214,276,0,0" TextWrapping="Wrap" Text="{Binding Genre}" VerticalAlignment="Top" Width="120" Grid.ColumnSpan="2" Grid.RowSpan="2"/>
</Grid>
</Window>
My problem is in t
he code behind the view when I need to add the newly inserted items from the textboxes along with the value from the combobox into the listbox which has "observ" List as Datasource.This is how the items are added to the listbox:
private void Button_Click(object sender, RoutedEventArgs e)
{
MediaList.Items.Add(txtArtist.Text);//this is where the error occurs
MediaList.Items.Add(txtGenre.Text);
MediaList.Items.Add(txtTitle.Text);
MediaList.Items.Add(mediaType.Text);
}
ObservableCollection<Multimedia> observ = new ObservableCollection<Multimedia>();
public MainWindow()
{
InitializeComponent();
MediaList.DataContext =this;
observ.Add(new Multimedia() { Title = "Coco Jambo", Artist = "Madonna", Genre = "Hip-Hop", Type = Multimedia.MediaType.CD });
observ.Add(new Multimedia() { Title = "Mercy", Artist = "Duffy", Genre = "Dance", Type = Multimedia.MediaType.DVD });
observ.Add(new Multimedia() { Title = "Live your life", Artist = "Rihanna", Genre = "Hip-Hop", Type = Multimedia.MediaType.BRay });
observ.Add(new Multimedia() { Title = "Oppss...I did it again", Artist = "Britney Spears", Genre = "Dance", Type = Multimedia.MediaType.CD });
observ.Add(new Multimedia() { Title = "Dead man walking", Artist = "Smiley", Genre = "Hip-Hop", Type = Multimedia.MediaType.DVD });
MediaList.ItemsSource = observ;
m.ListItems = observ;
}
The error that i am getting is related to the ItemsSource:"Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead."I have tried to clear the list before adding the elements like this:Media.Items.Clear() but without any luck.I have also tried to add directly to the list "observ" and clear it but still didn't work by throwing another error"Object not set to an instance of an object".Can anyone help me with this problem?
Best regards,
M
Continue reading...