Binding an ObservableCollection to multiple listviews not workingCatch multiple exceptions at once?ListBox vs. ListView - how to choose for data bindingLazy load of images in ListViewDetermining Which ListViewItem Checkbox existed inDatabinding issue with stopwatched elapsedC# WPF - Listview Binding not workingBinding 2 ObservableCollections to ListviewChanges in ObservableCollection do not update ListViewWPF Listbox binding problemsElement added to ObservableCollection in ViewModel not displayed

How can I discourage sharing internal API keys within a company?

What's the biggest organic molecule that could have a smell?

Is there a real-world mythological counterpart to WoW's "kill your gods for power" theme?

How to help my 2.5-year-old daughter take her medicine when she refuses to?

Maintenance tips to prolong engine lifespan for short trips

The Planck constant for mathematicians

Resume: How to quantify my contributions as a software engineer?

How can a Scotland-NI bridge break Brexit impasse?

Why would "an mule" be used instead of "a mule"?

Is there a reliable way to hide/convey a message in vocal expressions (speech, song,...)

How to work with a technician hired with a grant who argues everything

Telling my mother that I have anorexia without panicking her

Where can I get an anonymous Rav Kav card issued?

Why is the T-1000 humanoid?

Job offer without any details but asking me to withdraw other applications - is it normal?

Gas pipes - why does gas burn "outwards?"

How can I fix a framing mistake so I can drywall?

Does my opponent need to prove his creature has morph?

Leaving out pronouns in informal conversation

How to say "quirky" in German without sounding derogatory?

Can the UK veto its own extension request?

How can I locate a missing person abroad?

Are Democrats more likely to believe Astrology is a science?

I was promised a work PC but still awaiting approval 3 months later so using my own laptop - Is it fair to ask employer for laptop insurance?



Binding an ObservableCollection to multiple listviews not working


Catch multiple exceptions at once?ListBox vs. ListView - how to choose for data bindingLazy load of images in ListViewDetermining Which ListViewItem Checkbox existed inDatabinding issue with stopwatched elapsedC# WPF - Listview Binding not workingBinding 2 ObservableCollections to ListviewChanges in ObservableCollection do not update ListViewWPF Listbox binding problemsElement added to ObservableCollection in ViewModel not displayed






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















I'm new to programming and am having trouble figuring out how to get my ObservableCollection working on two different listviews across two different Windows in WPF. I think the problem is how I implemented my ObservableCollection, with the main culprit being this line here as shown in the SoundsWindow class:



ViewModel vm = this.DataContext as ViewModel;


This is how my listview displays my items added through:



vm.AddFile(fi.Name, file, 1);


The issue being that this does not update the other listview on the other window and neither is anything stored.



Heres my ViewModel:



public class ViewModel : INotifyPropertyChanged

public ViewModel()

SoundFiles = new ObservableCollection<SoundFile>();


public event PropertyChangedEventHandler PropertyChanged = delegate ;
private void OnPropertyChanged(string name)

PropertyChangedEventHandler handler = PropertyChanged;
handler(this, new PropertyChangedEventArgs(name));


public void AddFile(string fileName, string fileLocation, int groupID)

SoundFile soundfile = new SoundFile FileName = fileName, FileLocation = fileLocation, GroupID = groupID ;
SoundFiles.Add(soundfile);


private ObservableCollection<SoundFile> soundfiles;
public ObservableCollection<SoundFile> SoundFiles

get return soundfiles;
set

soundfiles = value;
OnPropertyChanged("SoundFiles");





SoundsWindow class:



public partial class SoundsWindow : Window
{
public SoundsWindow()

InitializeComponent();

this.DataContext = new ViewModel();



private void loadFileBtn1_Click(object sender, RoutedEventArgs e)

var ofd = new OpenFileDialog();
ofd.Multiselect = true;
ofd.Filter = "MP3 Files (*.mp3)


My SoundFile class:



public class SoundFile

public string FileName get; set;
public string FileLocation get; set;
public int GroupID get; set;



And the xaml:



<ListView x:Name="FilesList1" ItemsSource="Binding SoundFiles" HorizontalAlignment="Left" Height="379" Margin="10,35,0,0" VerticalAlignment="Top" Width="600">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="Binding Path=FileName" Width="230"/>
<GridViewColumn Header="Location" DisplayMemberBinding="Binding Path=FileLocation" Width="320"/>
<GridViewColumn Header="Group" DisplayMemberBinding="Binding Path=GroupID" Width="40"/>
</GridView>
</ListView.View>
</ListView>


For testing purposes the other window is a copy of SoundsWindow so that shouldnt be a problem.



As I pretty much have no idea what I'm doing iv checked about everything but am pretty sure the culprit is my implementation of how i add and store things in ObservableCollection. Any help would be immensely appreciated.










share|improve this question



















  • 4





    Obviously this.DataContext = new ViewModel(); is called in both windows. You do hence have two view model instances. Create a single one outside the Window constructor and pass it to the DataContext of both Windows.

    – Clemens
    Mar 28 at 8:40











  • Can we see the code you use to load the window please?

    – Robin Bennett
    Mar 28 at 8:42











  • Thanks for the reply, could you please show me how I would do that @Clemens . Thanks for the help.

    – zashen1
    Mar 28 at 8:46







  • 1





    One simple approach would be a static property in the ViewModel class, e.g. public static ViewModel Instance get; = new ViewModel();. Then assign the instance to a Window's DataContext by DataContext = ViewModel.Instance;

    – Clemens
    Mar 28 at 9:14







  • 1





    This is exactly what I needed. Thanks @Clemens you're a legend.

    – zashen1
    Mar 28 at 9:43

















2















I'm new to programming and am having trouble figuring out how to get my ObservableCollection working on two different listviews across two different Windows in WPF. I think the problem is how I implemented my ObservableCollection, with the main culprit being this line here as shown in the SoundsWindow class:



ViewModel vm = this.DataContext as ViewModel;


This is how my listview displays my items added through:



vm.AddFile(fi.Name, file, 1);


The issue being that this does not update the other listview on the other window and neither is anything stored.



Heres my ViewModel:



public class ViewModel : INotifyPropertyChanged

public ViewModel()

SoundFiles = new ObservableCollection<SoundFile>();


public event PropertyChangedEventHandler PropertyChanged = delegate ;
private void OnPropertyChanged(string name)

PropertyChangedEventHandler handler = PropertyChanged;
handler(this, new PropertyChangedEventArgs(name));


public void AddFile(string fileName, string fileLocation, int groupID)

SoundFile soundfile = new SoundFile FileName = fileName, FileLocation = fileLocation, GroupID = groupID ;
SoundFiles.Add(soundfile);


private ObservableCollection<SoundFile> soundfiles;
public ObservableCollection<SoundFile> SoundFiles

get return soundfiles;
set

soundfiles = value;
OnPropertyChanged("SoundFiles");





SoundsWindow class:



public partial class SoundsWindow : Window
{
public SoundsWindow()

InitializeComponent();

this.DataContext = new ViewModel();



private void loadFileBtn1_Click(object sender, RoutedEventArgs e)

var ofd = new OpenFileDialog();
ofd.Multiselect = true;
ofd.Filter = "MP3 Files (*.mp3)


My SoundFile class:



public class SoundFile

public string FileName get; set;
public string FileLocation get; set;
public int GroupID get; set;



And the xaml:



<ListView x:Name="FilesList1" ItemsSource="Binding SoundFiles" HorizontalAlignment="Left" Height="379" Margin="10,35,0,0" VerticalAlignment="Top" Width="600">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="Binding Path=FileName" Width="230"/>
<GridViewColumn Header="Location" DisplayMemberBinding="Binding Path=FileLocation" Width="320"/>
<GridViewColumn Header="Group" DisplayMemberBinding="Binding Path=GroupID" Width="40"/>
</GridView>
</ListView.View>
</ListView>


For testing purposes the other window is a copy of SoundsWindow so that shouldnt be a problem.



As I pretty much have no idea what I'm doing iv checked about everything but am pretty sure the culprit is my implementation of how i add and store things in ObservableCollection. Any help would be immensely appreciated.










share|improve this question



















  • 4





    Obviously this.DataContext = new ViewModel(); is called in both windows. You do hence have two view model instances. Create a single one outside the Window constructor and pass it to the DataContext of both Windows.

    – Clemens
    Mar 28 at 8:40











  • Can we see the code you use to load the window please?

    – Robin Bennett
    Mar 28 at 8:42











  • Thanks for the reply, could you please show me how I would do that @Clemens . Thanks for the help.

    – zashen1
    Mar 28 at 8:46







  • 1





    One simple approach would be a static property in the ViewModel class, e.g. public static ViewModel Instance get; = new ViewModel();. Then assign the instance to a Window's DataContext by DataContext = ViewModel.Instance;

    – Clemens
    Mar 28 at 9:14







  • 1





    This is exactly what I needed. Thanks @Clemens you're a legend.

    – zashen1
    Mar 28 at 9:43













2












2








2








I'm new to programming and am having trouble figuring out how to get my ObservableCollection working on two different listviews across two different Windows in WPF. I think the problem is how I implemented my ObservableCollection, with the main culprit being this line here as shown in the SoundsWindow class:



ViewModel vm = this.DataContext as ViewModel;


This is how my listview displays my items added through:



vm.AddFile(fi.Name, file, 1);


The issue being that this does not update the other listview on the other window and neither is anything stored.



Heres my ViewModel:



public class ViewModel : INotifyPropertyChanged

public ViewModel()

SoundFiles = new ObservableCollection<SoundFile>();


public event PropertyChangedEventHandler PropertyChanged = delegate ;
private void OnPropertyChanged(string name)

PropertyChangedEventHandler handler = PropertyChanged;
handler(this, new PropertyChangedEventArgs(name));


public void AddFile(string fileName, string fileLocation, int groupID)

SoundFile soundfile = new SoundFile FileName = fileName, FileLocation = fileLocation, GroupID = groupID ;
SoundFiles.Add(soundfile);


private ObservableCollection<SoundFile> soundfiles;
public ObservableCollection<SoundFile> SoundFiles

get return soundfiles;
set

soundfiles = value;
OnPropertyChanged("SoundFiles");





SoundsWindow class:



public partial class SoundsWindow : Window
{
public SoundsWindow()

InitializeComponent();

this.DataContext = new ViewModel();



private void loadFileBtn1_Click(object sender, RoutedEventArgs e)

var ofd = new OpenFileDialog();
ofd.Multiselect = true;
ofd.Filter = "MP3 Files (*.mp3)


My SoundFile class:



public class SoundFile

public string FileName get; set;
public string FileLocation get; set;
public int GroupID get; set;



And the xaml:



<ListView x:Name="FilesList1" ItemsSource="Binding SoundFiles" HorizontalAlignment="Left" Height="379" Margin="10,35,0,0" VerticalAlignment="Top" Width="600">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="Binding Path=FileName" Width="230"/>
<GridViewColumn Header="Location" DisplayMemberBinding="Binding Path=FileLocation" Width="320"/>
<GridViewColumn Header="Group" DisplayMemberBinding="Binding Path=GroupID" Width="40"/>
</GridView>
</ListView.View>
</ListView>


For testing purposes the other window is a copy of SoundsWindow so that shouldnt be a problem.



As I pretty much have no idea what I'm doing iv checked about everything but am pretty sure the culprit is my implementation of how i add and store things in ObservableCollection. Any help would be immensely appreciated.










share|improve this question














I'm new to programming and am having trouble figuring out how to get my ObservableCollection working on two different listviews across two different Windows in WPF. I think the problem is how I implemented my ObservableCollection, with the main culprit being this line here as shown in the SoundsWindow class:



ViewModel vm = this.DataContext as ViewModel;


This is how my listview displays my items added through:



vm.AddFile(fi.Name, file, 1);


The issue being that this does not update the other listview on the other window and neither is anything stored.



Heres my ViewModel:



public class ViewModel : INotifyPropertyChanged

public ViewModel()

SoundFiles = new ObservableCollection<SoundFile>();


public event PropertyChangedEventHandler PropertyChanged = delegate ;
private void OnPropertyChanged(string name)

PropertyChangedEventHandler handler = PropertyChanged;
handler(this, new PropertyChangedEventArgs(name));


public void AddFile(string fileName, string fileLocation, int groupID)

SoundFile soundfile = new SoundFile FileName = fileName, FileLocation = fileLocation, GroupID = groupID ;
SoundFiles.Add(soundfile);


private ObservableCollection<SoundFile> soundfiles;
public ObservableCollection<SoundFile> SoundFiles

get return soundfiles;
set

soundfiles = value;
OnPropertyChanged("SoundFiles");





SoundsWindow class:



public partial class SoundsWindow : Window
{
public SoundsWindow()

InitializeComponent();

this.DataContext = new ViewModel();



private void loadFileBtn1_Click(object sender, RoutedEventArgs e)

var ofd = new OpenFileDialog();
ofd.Multiselect = true;
ofd.Filter = "MP3 Files (*.mp3)


My SoundFile class:



public class SoundFile

public string FileName get; set;
public string FileLocation get; set;
public int GroupID get; set;



And the xaml:



<ListView x:Name="FilesList1" ItemsSource="Binding SoundFiles" HorizontalAlignment="Left" Height="379" Margin="10,35,0,0" VerticalAlignment="Top" Width="600">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="Binding Path=FileName" Width="230"/>
<GridViewColumn Header="Location" DisplayMemberBinding="Binding Path=FileLocation" Width="320"/>
<GridViewColumn Header="Group" DisplayMemberBinding="Binding Path=GroupID" Width="40"/>
</GridView>
</ListView.View>
</ListView>


For testing purposes the other window is a copy of SoundsWindow so that shouldnt be a problem.



As I pretty much have no idea what I'm doing iv checked about everything but am pretty sure the culprit is my implementation of how i add and store things in ObservableCollection. Any help would be immensely appreciated.







c# wpf listview mvvm observablecollection






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 28 at 8:37









zashen1zashen1

111 bronze badge




111 bronze badge










  • 4





    Obviously this.DataContext = new ViewModel(); is called in both windows. You do hence have two view model instances. Create a single one outside the Window constructor and pass it to the DataContext of both Windows.

    – Clemens
    Mar 28 at 8:40











  • Can we see the code you use to load the window please?

    – Robin Bennett
    Mar 28 at 8:42











  • Thanks for the reply, could you please show me how I would do that @Clemens . Thanks for the help.

    – zashen1
    Mar 28 at 8:46







  • 1





    One simple approach would be a static property in the ViewModel class, e.g. public static ViewModel Instance get; = new ViewModel();. Then assign the instance to a Window's DataContext by DataContext = ViewModel.Instance;

    – Clemens
    Mar 28 at 9:14







  • 1





    This is exactly what I needed. Thanks @Clemens you're a legend.

    – zashen1
    Mar 28 at 9:43












  • 4





    Obviously this.DataContext = new ViewModel(); is called in both windows. You do hence have two view model instances. Create a single one outside the Window constructor and pass it to the DataContext of both Windows.

    – Clemens
    Mar 28 at 8:40











  • Can we see the code you use to load the window please?

    – Robin Bennett
    Mar 28 at 8:42











  • Thanks for the reply, could you please show me how I would do that @Clemens . Thanks for the help.

    – zashen1
    Mar 28 at 8:46







  • 1





    One simple approach would be a static property in the ViewModel class, e.g. public static ViewModel Instance get; = new ViewModel();. Then assign the instance to a Window's DataContext by DataContext = ViewModel.Instance;

    – Clemens
    Mar 28 at 9:14







  • 1





    This is exactly what I needed. Thanks @Clemens you're a legend.

    – zashen1
    Mar 28 at 9:43







4




4





Obviously this.DataContext = new ViewModel(); is called in both windows. You do hence have two view model instances. Create a single one outside the Window constructor and pass it to the DataContext of both Windows.

– Clemens
Mar 28 at 8:40





Obviously this.DataContext = new ViewModel(); is called in both windows. You do hence have two view model instances. Create a single one outside the Window constructor and pass it to the DataContext of both Windows.

– Clemens
Mar 28 at 8:40













Can we see the code you use to load the window please?

– Robin Bennett
Mar 28 at 8:42





Can we see the code you use to load the window please?

– Robin Bennett
Mar 28 at 8:42













Thanks for the reply, could you please show me how I would do that @Clemens . Thanks for the help.

– zashen1
Mar 28 at 8:46






Thanks for the reply, could you please show me how I would do that @Clemens . Thanks for the help.

– zashen1
Mar 28 at 8:46





1




1





One simple approach would be a static property in the ViewModel class, e.g. public static ViewModel Instance get; = new ViewModel();. Then assign the instance to a Window's DataContext by DataContext = ViewModel.Instance;

– Clemens
Mar 28 at 9:14






One simple approach would be a static property in the ViewModel class, e.g. public static ViewModel Instance get; = new ViewModel();. Then assign the instance to a Window's DataContext by DataContext = ViewModel.Instance;

– Clemens
Mar 28 at 9:14





1




1





This is exactly what I needed. Thanks @Clemens you're a legend.

– zashen1
Mar 28 at 9:43





This is exactly what I needed. Thanks @Clemens you're a legend.

– zashen1
Mar 28 at 9:43












0






active

oldest

votes










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);














draft saved

draft discarded
















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55393193%2fbinding-an-observablecollection-to-multiple-listviews-not-working%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes




Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.







Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.




















draft saved

draft discarded















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55393193%2fbinding-an-observablecollection-to-multiple-listviews-not-working%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript