WPF DataGrid columns: how to manage event of value changing

纵然是瞬间 提交于 2019-12-30 11:01:08

问题


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:


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}" ... />


来源:https://stackoverflow.com/questions/18720892/wpf-datagrid-columns-how-to-manage-event-of-value-changing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!