WPF Databinding Question

Status
Not open for further replies.
So I'm (relatively) new to Linq and WPF (we are a .NET shop at work but no plans to start utilizing these technologies on a large scale in the near future) so I decided to write a little widget using them.

Everything is going alright for me except for one aspect: databinding a listview so that when I update a value in the database backend, it propagates back to the listview.

Some background:

*Using Linq to Entities 4.0(Relevant database tables mapped using *.edmx file)

*"Binding" the listview to entries in a database table:
Code:
listViewTasks.ItemsSource = tasks;//tasks is a generic list of Database table entries

*When listview entries are highlighted and a button is clicked, a column in the table is updated, but my list view doesn't get that update.
**EDIT: The column that gets updated is a foreign key on the table (a status value). Basically, I am wanting to reset tasks that are in error to try again.

I've set the DataBinding for listViewTasks to OneWay in the XAML.

What am I doing wrong/missing? What my issue really boils down to is how do I bind to a collection of items in XAML when I don't know what that collection will contain at compile-time?

Thanks for tolerating such remedial WPF questions.
 
BlackHex":2xbxhbvv said:
"tasks" must either a) be an ObservableCollection<T> or b) be a class that implements INotifyCollectionChanged and INotifyPropertyChanged.

"Tasks" is a List<T> where T is an EntityObject, which by drilling down does implement INotifyPropertyChanged via StructuralObject, so I should be covered there, shouldn't I?
 
Horatio":3uwbc9e7 said:
I don't think ObservableCollection will work for this scenario - if you want to update the UI when the database itself is updated, I think you have to manually refresh the items collection. I've never worked with L2E so I might be utterly wrong about that though.

You are correct about the ObservableCollection not solving the issue, but I thought the whole point of databinding was to have the UI automatically be updated when the backend changes?
 

ShuggyCoUk

Ars Legatus Legionis
10,388
Subscriptor++
but I thought the whole point of databinding was to have the UI automatically be updated when the backend changes?

Yes, but only for back ends where this is supported, and supporting this in general against an arbitrary db would be very expensive (in load terms and implementation terms).

Knowing about changes to cached local copies is one thing, knowing about changes in your db is another. I know of no db with this sort of ability in any language without some tool constantly polling for you in the background.
 
ShuggyCoUk":13b5xnx5 said:
but I thought the whole point of databinding was to have the UI automatically be updated when the backend changes?

Yes, but only for back ends where this is supported, and supporting this in general against an arbitrary db would be very expensive (in load terms and implementation terms).

Knowing about changes to cached local copies is one thing, knowing about changes in your db is another. I know of no db with this sort of ability in any language without some tool constantly polling for you in the background.

I have my EntityObject class that is correctly firing the OnStatusIDChanged() method, shouldn't that be enough to get this to work or am I not understanding databinding correctly?

I added a call to listViewTasks.Items.Refresh() at the completion of my update code, and that did correctly refresh the displayed status, am I just going to have to live with that?

This project is 80% academic for me, so I won't stress too much if that's the way it has to be, but to be able to push this little, useful widget forward to my coworkers/management as a reason why we may want to move forward with these new technologies, I'd like to have a little more understanding about what is going on here.

Thanks a lot for all of the helpful responses. I can't believe I lurked here for so long without harnessing the collective wisdom/knowledge of this forum with my own questions. I'll try not to abuse the power ;)
 

ShuggyCoUk

Ars Legatus Legionis
10,388
Subscriptor++
I have my EntityObject class that is correctly firing the OnStatusIDChanged() method, shouldn't that be enough to get this to work or am I not understanding databinding correctly?

This would fire only if the value changed from some impetus within your app. This could be from refreshing the underlying data source, or from some other change, but not because someone else somewhere updated the value in the database sorry.

Calling refresh every so often is likely your only option so long as the data doesn't take too long to pull from the db that should be fine. Just don't think it is doing anything 'clever' to make it efficient and design your app accordingly.
 
ShuggyCoUk":161n4qrf said:
I have my EntityObject class that is correctly firing the OnStatusIDChanged() method, shouldn't that be enough to get this to work or am I not understanding databinding correctly?

This would fire only if the value changed from some impetus within your app. This could be from refreshing the underlying data source, or from some other change, but not because someone else somewhere updated the value in the database sorry.

Calling refresh every so often is likely your only option so long as the data doesn't take too long to pull from the db that should be fine. Just don't think it is doing anything 'clever' to make it efficient and design your app accordingly.

Ah, I should have made it clear that my app is the impetus for the change to the status. I am basically doing this:

Code:
//t == task
t.Status = entities.Status.First(status => status.StatusID == Status.New);

Don't focus on how I'm getting the status (unless it really could be affecting what I am seeing) as the reason why I have to do it that way is the result of a stupid design decision I made some years ago.
 
TheJet":kqyh6pjs said:
I think the problem you're seeing is that your UI isn't updating because you're binding to the navigation property and not the underlying ID property. Can you override the OnStatusIDChanged() method and also fire a RaisePropertyChanged("Status")?

Awesome! That led me to it.

In the setter of the StatusID property, I added this call:

OnPropertyChanged("Status");

And that did it.

Stackoverflow also provided some assistance after you mentioned the navigation property issue:

http://stackoverflow.com/questions/1977274/binding-doesnt-work-for-navigation-properties

Thanks a lot for your help everyone.
 
I got my widget more or less working (and appreciated by some coworkers who actually find it useful).

Now I'm really going head-first into WPF and trying to rewrite my widget using MVVM. So far, so good, except for a problem with a combo box that allows the user to select a database to work with. From what I read, to associate a command with a combo box (i.e. to fire a command when the combo box's selected item changes), I needed to derive a new combo box class that implements ICommand. I did that without any issue, but now I'm running into a problem when binding to my derived combo box. When I bind the ItemsSource--via XAML--to a property on my VM, I get a runtime error of "Items collection must be empty before using ItemsSource." As soon as I comment out that line, the app loads without issue (it just doesn't have any databases to choose from :)).

Anybody have any idea what I am doing wrong here? Do I have to override more aspects of the combo box? I saw this page, but I don't think it is applicable to my situation since I am not wrapping a combo box, just overriding it. I also looked at this and this, but neither of those are applicable.

Here's the XAML for my combobox:
Code:
<UI:ComboBoxWithCommand Text="{Binding JobLoadingStatus}" 
ItemsSource ="{Binding Jobs}" Command="{Binding SelectJobCommand}" 
CommandParameter="{Binding SelectedItem}" 
Height="23" Margin="5,5,5,5" Width="250" VerticalAlignment="Top"  
ItemContainerStyle="{StaticResource comboBoxJobStyle}" TabIndex="1" 
HorizontalAlignment="Stretch" DisplayMemberPath="DatabaseName" />

I followed the example here for creating my custom combo box, except I am overriding OnSelectionChanged() instead of OnValueChanged().

Thanks for the assistance in advance.

EDIT: broke up the XAML so there are some line breaks
 
I figured it out almost as soon as I posted this. Simply by writing that I followed the example given by Microsoft, I went back and surprise, surprise, I missed one aspect: I created my override as a UserControl, when actually I should have just created it as a normal class. Once I recreated it doing that, it worked. Hurray!!!

From now on, whenever I have a problem, I should just post it here since it seems like doing so usually makes me rethink what I was doing and therefore coming up with the solution myself.
 
Status
Not open for further replies.