WPF DataGrid 列:如何管理值更改事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18720892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:37:20  来源:igfitidea点击:

WPF DataGrid columns: how to manage event of value changing

wpfeventsdatagrid

提问by FrancescoDS

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

在我的 WPF C# 项目中,我有一个 Datagrid,如下所示:

<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.

我需要一个事件来管理第二列和第三列中的值变化(即“%”和“Allineamento barre”)。不需要插入的值,我只需要在更改其中一个值时引发一个事件。我该如何执行?我需要定义事件方法的方法,我可以在其中定义一些操作。我读过这篇如何在 wpf 数据网格的单元格中的值使用 MVVM 更改时引发事件?但我没有链接到数据网格的可观察集合。

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

编辑:Datagrid ItemSource 与以下对象链接:

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

谢谢

回答by Sheridan

You seemto 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.

似乎是从 WinForms 的角度来看这个问题的。在 WPF 中,我们通常更喜欢操作数据对象而不是 UI 对象。你说ObservableCollection<T>你的物品没有一个,但我建议你使用一个。

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 INotifyPropertyChangedinterface in it.

如果您的数据没有数据类型类,那么我建议您创建一个。然后,您应该INotifyPropertyChanged在其中实现接口。

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

执行此操作并将您的集合属性设置为 的ItemsSource之后DataGrid,您需要做的就是将INotifyPropertyChanged处理程序附加到您选择的数据类型:

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