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

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현