locked
DataForm: How to stop dataform selecting first item RRS feed

  • Question

  • I have this annoying problem that I want to load a dataform and then select the 3rd record (this is a value passed into my page.

    Unfortunately in practice this seems to be a very complex problem.  What actually happens is that you load a collection

    Incidents = new ObservableCollection<Incident>(incidents.Entities);


    And then databinding causes the dataform to then select the first item from this list!  Meanwhile your code is setting the up the field that your currentItem is bound to.

    CurrentIncident = item;

    So, when you run your app you MAY get the 1st record, or you may get the 3rd depending on how quick things are running!

    Is there a way to make the DataForm not set the currentitem when loaded?

    If not how would I go about solving this problem?

    cheers


    Monday, November 15, 2010 10:13 PM

Answers

  • Yes, the behaviour of the CurrentItem and CurrentIndex bindings is as expected. OneWay binding is just what it says, a one way. It means that the information goes from the Binded property to the control but not back. This is why you don't see the changes. And for the info to go even that way the properties need to implement INotifyPropertyChanged. You should definitely use TwoWay binding for these properties. 

    You're may be correct that this is something related to the slow performing VM. If you don't get it to work, I may also do a sample app for you.

    Wednesday, November 17, 2010 12:57 AM

All replies

  • You must have something else on your code that's messing up the selection. The DataForm probably selects the first by default but if you set the selecteditem correctly it should always select the correct one as soon as it gets set. Maybe the problem is that CurrentIncident doesn't notify the changes to the DataForm? (INotifyPropertyChanged)

    Tuesday, November 16, 2010 12:43 AM
  • Thanks,

    The notifications are working (The order which the notifications occur seem to be at fault, as sometimes it works, and sometimes it does not).  

    I have turned of the twoway databinding and confirm that

    A\ the 'resetting' of the selected item originates from the xaml.

    B\ the CurrentIncident in my viewmodel is the value I expect.


    However doing that little experiment has uncovered an interesting feature. notifications to the dataform index/currentitem do not work at all when I set my databinding to oneway (but when I set to two way they do work).  I guess at this point as you have confirm that this is not the usual behavior, I will have to make a demo project up.

            <toolkit:DataForm Grid.Column="1" x:Name="uxIncidents"                                                         
                              ItemsSource="{Binding Incidents, Mode=TwoWay}" 
                              AutoGenerateFields="False" 
                              AutoEdit="False"                           
                              AutoCommit="False"                             
                              Header="{Binding IncidentCountCaption}"
                              EditEnding="uxIncidents_EditEnding"
                              EditEnded="uxIncidents_EditEnded"
                              BeginningEdit="uxIncidents_BeginningEdit"  
                              AddingNewItem="uxIncidents_AddingNewItem"                                
                              CommandButtonsVisibility="Add,Cancel,Commit,Edit,Navigation"    
                              CurrentItem ="{Binding CurrentIncident, Mode=OneWay}"
                              CurrentIndex="{Binding IncidentIndex, Mode=OneWay}">


    NOTE: I have noticed odd issues running on a poor performing VM before, am beginning to think think this may be my problem. have discovered that the view is returning a Index value of 0 AFTER my viewmodel's loaded event.


    Tuesday, November 16, 2010 2:44 PM
  • Yes, the behaviour of the CurrentItem and CurrentIndex bindings is as expected. OneWay binding is just what it says, a one way. It means that the information goes from the Binded property to the control but not back. This is why you don't see the changes. And for the info to go even that way the properties need to implement INotifyPropertyChanged. You should definitely use TwoWay binding for these properties. 

    You're may be correct that this is something related to the slow performing VM. If you don't get it to work, I may also do a sample app for you.

    Wednesday, November 17, 2010 12:57 AM
  • Thanks for your help.  

    I found the issue, it was all my fault ;(  The view is a relativity complex one, and I have a lot of asynchronous  calls going on + a many-many relationship... this combination made it hard to work out what was actually happening.  Which was after I loaded the first control, the second then reloaded the first control...  I am definatly going to use a framework that does coroutines next time (Already started a demo with Caliburn.Micro)

    Wednesday, November 17, 2010 2:13 PM
  • Great that you got it working! Happy coding!

    Thursday, November 18, 2010 1:25 AM