2
votes

In my WPF C# project, I have a Datagrid as follows:

<DataGrid x:Name="FixedPositionDataGrid" HorizontalAlignment="Left" Margin="33,229,0,0" VerticalAlignment="Top" Width="172" Height="128" AutoGenerateColumns="False" FontSize="10" CanUserAddRows="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="indice" Binding="{Binding index}" IsReadOnly="True"/>
            <DataGridTextColumn Header="%" Binding="{Binding percentage}" />                                    
            <DataGridComboBoxColumn x:Name="DataGridComboBoxColumnAlignment" Header="Allineamento barre" SelectedValueBinding="{Binding alignment}"/>
        </DataGrid.Columns>
    </DataGrid>

I need to have an event that manages the value changing in second and third columns (that is "%" and "Allineamento barre"). No need about the value inserted, I just need to raise an event when one of values are changed. How can I perform it? I need the way to define the event method in which I can define some operations to do. I've read this how to raise an event when a value in a cell of a wpf datagrid changes using MVVM? but I don't have an observable collection linked to datagrid.

EDIT: The Datagrid ItemSource is linked with the following objects:

public class FixedPosition
{
    [XmlAttribute]
    public int index { get; set; }

    public int percentage { get; set; }
    public HorizontalAlignment alignment { get; set; }        
}

How can I modify it to obtain the result expected?

Thanks

1
Why can't you just do what you want in the setter of percentage and/or alignment property? That's what is triggered when value changesdkozl
@dkozl I need to know how to do it...can you give me an example please?FrancescoDS
Since you can modify percentage I assume it has getter get and setter set so trigger what you want to trigger in the setter which will be whenever percentage value has been set: set { ... [trigger action] }dkozl
@dkozl I need a clear example...lok at my edit and please give an example based on it. ThanksFrancescoDS

1 Answers

4
votes

You seem to be looking at this problem from a WinForms perspective. In WPF, we generally prefer to manipulate data objects rather than UI objects. You said that don't have an ObservableCollection<T> for your items, but I would recommend that you use one.

If you don't have a data type class for your data, then I'd advise you to create one. You should then implement the INotifyPropertyChanged interface in it.

After doing this and setting your collection property as the ItemsSource of your DataGrid, then all you need to do is to attach an INotifyPropertyChanged handler to your selected data type:

In the view model:

public ObservableCollection<YourDataType> Items
{
    get { return items; }
    set { items = value; NotifyPropertyChanged("Items"); }
}

public YourDataType SelectedItem
{
    get { return selectedItem; }
    set { selectedItem = value; NotifyPropertyChanged("SelectedItem"); }
}

In the view model constructor:

SelectedItem.PropertyChanged += SelectedItem_PropertyChanged;

In the view model:

private void SelectedItem_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    // this will be called when any property value of the SelectedItem object changes
    if (e.PropertyName == "YourPropertyName") DoSomethingHere();
    else if (e.PropertyName == "OtherPropertyName") DoSomethingElse();
}

In the UI:

<DataGrid ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" ... />