Вопрос

I've looked and can't quite find what I'm looking for. I have a MVVM environment. On the View model, I have a Datatable available from data from a database connection/query. I've exposed a property (getter/setter) as a "DataView" based on "TheTable.DefaultView".

I have a datagrid in the window that binds to the dataview... no problem.

<DataGrid AutoGenerateColumns="False" 
   Name="dataMyData"
   ItemsSource="{Binding Path=ViewModelViewProperty, 
   NotifyOnSourceUpdated=True, 
   NotifyOnTargetUpdated=True}"
   SelectedItem="{Binding Path=JustOneRecordInView, Mode=TwoWay}"
   SelectionMode="Single"
   SelectionUnit="FullRow"
   GridLinesVisibility="Horizontal"
   CanUserDeleteRows="False"
   CanUserAddRows="False" >

For the "SelectedItem" above, it too comes from a property exposed on the ViewModel via its (getter/setter).

Now, my problem. As I scroll down the list of records in the data grid, I have other textbox controls to show more data than just the grid listing provides. I want to be able to edit the data of "the current row", so I have a textbox with as many settings as I can think of, but something is still amiss.

<TextBox 
   Text="{Binding Path=PropertyForCurrentRecord[SpecificColumnInDataViewRow], 
        Mode=TwoWay, 
        UpdateSourceTrigger=PropertyChanged, 
        ValidatesOnDataErrors=True, 
        ValidatesOnExceptions=True,
        BindsDirectlyToSource=True,
        NotifyOnSourceUpdated=True,
        NotifyOnTargetUpdated=True,
        NotifyOnValidationError=True}"
    Name="textBox1" VerticalAlignment="Top" Width="40" />

If I scroll though and am in an edit mode of the data, and change the value in the textbox associated with the current row, AND this value is one of the columns being displayed in the grid, the datagrid itself DOES NOT show the changed value. However, if I DO continue to scroll off and back on the same record, the value in the textbox DOES show the changed to value.

So, how can I force that the grid data source is considered updated too as the individual column from the specific row has changed, and the grid itself updated too. Thanks...

Это было полезно?

Решение 2

WOW... after a bunch more digging from the days prior, I finally cracked it and here is how I fixed it.

private bool AcceptingTheChanges = false;
private DataRowView myRecord;
public DataRowView MyRecord
{
   get { return myRecord; }
   set {
          if (myRecord != null)
                myRecord.Row.Table.AcceptChanges();

          // Now, get the incoming value and re-store into private
          myRecord = value;
          // Finally, raise event that it changed to refresh window...
          RaisePropertyChanged("MyRecord");
       }
}

Другие советы

Your datagrid is bound to a collection of some elements of type SomeType. To make your task you need to implement INotifyPropertyChanged in the SomeType (or inherit from ViewModelBase if you have it). You can take a look at a good sample here: http://www.hightech.ir/SeeSharp/best-implementation-of-inotifypropertychange-ever

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top