Reputation: 8768
I have an observable collection with a custom class called songInfo
inside.
It is bound to a ListView
. Here is the code for the binding:
C#:
var songData = new ObservableCollection<songInfo>();
public ObservableCollection<songInfo> _songData
{
get
{
return songData;
}
}
public class songInfo
{
public string Title { get; set; }
public string Artist { get; set; }
public string Album { get; set; }
public string Location { get; set; }
public string Ext { get; set; }
public bool isSongPlaying { get; set; }
}
XAML:
<Window x:Class="Genesis.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Genesis (Alpha)" Height="897" Width="882" Loaded="Window_Loaded"
DataContext="{Binding RelativeSource={RelativeSource Self}}" Name="Genesis">
<ListView Margin="12,39,0,0" Name="Library" DataContext="{Binding}" ItemsSource="{Binding _songData}" Height="681" VerticalAlignment="Top" MouseDoubleClick="Library_MouseDoubleClick" ContextMenu="{StaticResource MyContextMenu}" AlternationCount="2" Background="AliceBlue" HorizontalAlignment="Left" Width="846">
<ListView.View>
<GridView x:Name="gvLibrary">
<GridViewColumn Width="20" Header="hi" DisplayMemberBinding="{Binding isSongPlaying}" x:Name="gvColumnPlaying" />
<GridViewColumn Width="220" Header="Title" DisplayMemberBinding="{Binding Title}" x:Name="gvColumnTitle" />
<GridViewColumn Width="180" Header="Artist" DisplayMemberBinding="{Binding Artist}" x:Name="gvColumnArtist" />
<GridViewColumn Width="180" Header="Album" DisplayMemberBinding="{Binding Album}" x:Name="gvColumnAlbum" />
<GridViewColumn Width="180" Header="Location" DisplayMemberBinding="{Binding Location}" x:Name="gvColumnLocation" />
<GridViewColumn Width="80" Header="File Type" DisplayMemberBinding="{Binding Ext}" x:Name="gvColumnFileType" />
</GridView>
</ListView.View>
</ListView>
The songInfo is populated at other points in my code. When elements are added or removed, the ListView is updated. However, there are points when I simply change just songInfo.Ext
or songInfo.Location
, etc. I found a convoluted way to do this and update, but I had to remove the element and re-add it:
songInfo temp = songData[playing_song_index];
songData.RemoveAt(playing_song_index);
songData.Insert(playing_song_index, new songInfo()
{
Title = temp.Title,
Artist = temp.Artist,
Album = temp.Album,
Location = temp.Location,
Ext = temp.Ext,
isSongPlaying = true
});
That changes isSongPlaying to true.
Is there an easier way to just update one "column" of the GridView?
Upvotes: 3
Views: 4511
Reputation: 38385
If songInfo doesn't use INotifyPropertyChanged, then what you are doing will work. You could clean it up a bit by:
var mySong = songData.//get your song
int index = songData.IndexOf( mySong );
songData.Remove( mySong );
songData.Insert( index, mySong );
Upvotes: 3
Reputation: 564691
Your songInfo
class needs to implement INotifyPropertyChanged
.
If you implement this interface properly, then changes to members of the class will automatically get reflected within the user interface via the binding.
Upvotes: 6