WPF DataGrid 列:如何管理值更改事件 [英] WPF DataGrid columns: how to manage event of value changing
问题描述
在我的 WPF C# 项目中,我有一个 Datagrid,如下所示:
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>
我需要一个事件来管理第二列和第三列中的值变化(即%"和Allineamento barre").不需要插入的值,我只需要在更改其中一个值时引发一个事件.我该如何执行?我需要定义事件方法的方法,我可以在其中定义一些操作.我读过这个 如何在 wpf 数据网格的单元格中的值使用 MVVM 更改时引发事件? 但我没有链接到数据网格的可观察集合.
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.
Datagrid ItemSource 与以下对象链接:
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?
谢谢
推荐答案
您似乎从 WinForms 的角度看待这个问题.在 WPF 中,我们通常更喜欢操作数据对象而不是 UI 对象.你说你的项目没有 ObservableCollection
,但我建议你使用一个.
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.
如果您的数据没有数据类型类,那么我建议您创建一个.然后,您应该在其中实现 INotifyPropertyChanged
接口.
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.
执行此操作并将您的集合属性设置为 DataGrid
的 ItemsSource
后,您需要做的就是附加一个 INotifyPropertyChanged
处理您选择的数据类型:
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:
在视图模型中:
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;
在视图模型中:
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();
}
在用户界面中:
<DataGrid ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" ... />
这篇关于WPF DataGrid 列:如何管理值更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!