Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. This is my current situation. When I select a single row from my datagrid, and I click on the button, the cell must change value, but it doesn't change anything

XML
<DataGrid
    ItemsSource="{Binding Dati_Viaggio, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}"
    SelectedItem="{Binding SelectDati_Viaggio, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}"

    SelectionMode="Single" CanUserAddRows="False"  CanUserDeleteRows="False"
    AutoGenerateColumns="False" HorizontalAlignment="Left" Height="119" Margin="10,10,0,0" VerticalAlignment="Top" Width="497">

    <DataGrid.Columns>
        <DataGridTextColumn x:Name="NumOrd"
            Binding="{Binding Path=NumOrd, Mode=TwoWay, NotifyOnSourceUpdated=True,
            UpdateSourceTrigger=PropertyChanged}"  Header="NumOrd" Width="150" />
    </DataGrid.Columns>
</DataGrid>



And this the vb code

VB
Private Property _NumOrd As String
Public Property NumOrd As String
    Get
        Return _NumOrd
    End Get
    Set(value As String)
        _NumOrd = value
        OnPropertyChanged("NumOrd")
    End Set
End Property

Public Property Dati_Viaggio As ObservableCollection(Of Model_Ricerca_Dati_Viaggio)
Private _SelectDati_Viaggio As Model_Ricerca_Dati_Viaggio
Public Property SelectDati_Viaggio As Model_Ricerca_Dati_Viaggio
    Get
        Return _SelectDati_Viaggio
    End Get
    Set(value As Model_Ricerca_Dati_Viaggio)
        _SelectDati_Viaggio = value
        OnPropertyChanged("SelectDati_Viaggio")

    End Set
End Property


Why, if I write this code the cell doesn't refresh ?

VB
SelectDati_Viaggio.NumOrd = Now.String

OnPropertyChanged("NumOrd")
OnPropertyChanged("Dati_Viaggio")
OnPropertyChanged("SelectDati_Viaggio")
Posted

1 solution

It looks that you have absolutely no idea what you are doing.

First, read some documentation about WPF Binding https://msdn.microsoft.com/en-us/library/ms752347.aspx[^] - how it works, what is binding target, binding source and how should it be used. Articles about MVVM sure come in handy.

The main problem (from the code you provided) is that the NumOrd property should be on the Model_Ricerca_Dati_Viaggio class, since DataGrid is bound to the collection of Model_Ricerca_Dati_Viaggios.
Then, you don't need to explicitly invoke none of the OnPropertyChanged in your button click event handler.
Don't forget to set the control's DataContext to the instance of class, which contains Dati_Viaggio and SelectDati_Viaggio properties.

Good habbit is to write code in english and not your native language.
Also by convention, .NET clases should not contain underscores (_).
 
Share this answer
 
Comments
antobaro 28-Feb-15 6:36am    
Hey teacher... what does mean ....Good habbit ???? !!!!!
Matej Hlatky 28-Feb-15 7:17am    
Sorry, I meant "habit" with one B. It is better to write code in English, especially when you are going to share it on the internet.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900