-
April 14th, 2014, 04:18 PM
#1
Changing Lists in WPF problem
I am populating a list out of entity framework from a database. However I have to get live data from a piece of hardware and then selectively add the data to the database. I have the itemsource set in my list but when hydrating the collection from a new source if does not update the listview. The data is refreshed on an event from the hardware where I update the RaiseNotifyChanged on the observable list.
can someone help
Here is the code for the View Model
public ObservableCollection<RadGridView.Database.MeterData> MeterDbData
{
get
{
if(bLoadFromDb == true)
{
if (meterDbData == null)
return null;
meterDbData = new ObservableCollection<MeterData>(
trueDbContext.MeterDatas.ToList<MeterData>());
return meterDbData;
}
return meterDbData;
}
set
{
if(bLoadFromDb == true)
{
meterDbData = new ObservableCollection<MeterData>(
trueDbContext.MeterDatas.ToList<MeterData>());
}
else
{
meterDbData = value;
}
this.RaisePropertyChanged("MeterDbData");
}
}
public void MeterHardware_OnFinsihedDownloadMeterUpdateUI(object sender, MeterEventArgs e)
{
bLoadFromDb = false;
MeterDbData = LoadDataFromMeter();
}
private ObservableCollection<RadGridView.Database.MeterData> LoadDataFromMeter()
{
ObservableCollection<RadGridView.Database.MeterData> newlist =
new ObservableCollection<MeterData>();
int count = MeterHardware.ResultsList.Count;
for(int i=0; i < count; i++)
{
newlist.Add(new RadGridView.Database.MeterData()
{
MisTimeStamp = meterHardware.ResultsList[i].TimeStamp,
MisValue = Convert.ToDecimal(meterHardware.ResultLst[i].Value),
});
}
return newlist;
}
Main window XAML
<Window.Resources>
</Window.Resources>
<Window.DataContext>
<vm:TrendViewModel />
</Window.DataContext>
<Controls:MeterResultsListViewUserControl1 HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="lvDatav" Grid.Row="2" Margin="10,129,10,-129"/>
NOW CONTROL XAML
<Control.DataContext>
<vm:TrendViewModel />
</Control.DataContext>
<ListView Margin="10" ItemsSource="{Binding MeterDbData}" >
<ListView.View>
<GridView>
<GridViewColumn Header="TimeStamp" Width="120" DisplayMemberBinding="{Binding MisTimeStamp}" />
<GridViewColumn Header="Result" Width="120" DisplayMemberBinding="{Binding MisValue}" />
</GridView>
</ListView.View>
</ListView>
-
April 14th, 2014, 04:45 PM
#2
Re: Changing Lists in WPF problem
You are creating a new observablecollection when you change the data. Instead, update the existing ObservableCollection instance. That should cause its change event to fire.
Also, make sure the OnFinishDownloadMeterUpdate handler is running on the main UI thread.
-
April 14th, 2014, 05:26 PM
#3
Re: Changing Lists in WPF problem
Originally Posted by Arjay
You are creating a new observablecollection when you change the data. Instead, update the existing ObservableCollection instance. That should cause its change event to fire.
Also, make sure the OnFinishDownloadMeterUpdate handler is running on the main UI thread.
Ok. I will try it. However it has not worked in the past. The OnFinishDownloadMeterUpdate event is part of the view model and the hardware item is an object of that model right now. Trying to figure out the best way to do this as. I may need other view models to have access to the hardware object and this messaging event. there is only one event and its a bad state engine for processing data from the hardware.
-
April 14th, 2014, 05:27 PM
#4
Re: Changing Lists in WPF problem
As I mentioned, make sure that the hardware handler is getting called in the UI thread.
-
April 15th, 2014, 07:27 AM
#5
Re: Changing Lists in WPF problem
Ho do I make sure of that? the viewmodel is tied to the main window. I will need other viewmodels as well for other controls and do not know the best way to do that. Most people make a collection of them. the I think setting up the paths in the binding to the elements. I'm kinda new to WPF and this is for work. I ran into a lot of problems with the lists and errors saying "you cant update collection from another thread". tried concurrent bad and that did not work. Could use a good example of this. Also one for applying a theme to a control. I'm also learning blend as well to them this App which will be a Windows Store app eventually.
-
April 15th, 2014, 11:12 AM
#6
Re: Changing Lists in WPF problem
Search bing for "Update the UI in WPF from another thread". Look at the articles that mention the dispatcher. As for determining if you are running in a different thread, Use Thread.CurrentThread.ThreadId in the handler and main UI thread and compare the results (btw, this is for debugging only, you don't need to leave it in there after you've determine if the threadIds match).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|