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;
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
add a comment
|
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
4
Obviouslythis.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 byDataContext = 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
add a comment
|
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
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
c# wpf listview mvvm observablecollection
asked Mar 28 at 8:37
zashen1zashen1
111 bronze badge
111 bronze badge
4
Obviouslythis.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 byDataContext = 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
add a comment
|
4
Obviouslythis.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 byDataContext = 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
add a comment
|
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 byDataContext = 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