WPF DataGrid列:如何管理值更改的事件 [英] WPF DataGrid columns: how to manage event of value changing

查看:268
本文介绍了WPF DataGrid列:如何管理值更改的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 < DataGrid x:Name =FixedPositionDataGrid Horizo​​ntalAlignment =LeftMargin =33,229,0,0VerticalAlignment =TopWidth =172Height =128AutoGenerateColumns =FalseFontSize =10CanUserAddRows =False> 
< DataGrid.Columns>
< DataGridTextColumn Header =indiceBinding ={Binding index}IsReadOnly =True/>
< DataGridTextColumn Header =%Binding ={Binding percentage}/>
< DataGridComboBoxColumn x:Name =DataGridComboBoxColumnAlignmentHeader =Allineamento barreSelectedValueBinding ={Binding alignment}/>
< /DataGrid.Columns>
< / DataGrid>

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

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

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

public int percentage {get;组; }
public Horizo​​ntalAlignment alignment {get;组; }
}

我如何修改它以获得预期的结果?



谢谢

解决方案

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



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



执行此操作并将您的集合属性设置为 DataGrid code>,那么你需要做的就是将一个 INotifyPropertyChanged 处理程序附加到你选择的数据类型中:



在视图模型中:

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

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

在视图模型构造函数中:

  SelectedItem.PropertyChanged + = SelectedItem_PropertyChanged; 

在视图模型中:

  private void SelectedItem_PropertyChanged(object sender,PropertyChangedEventArgs e)
{
//当SelectedItem对象的任何属性值更改
时,这将被调用if(e。 PropertyName ==YourPropertyName)DoSomethingHere();
else if(e.PropertyName ==OtherPropertyName)DoSomethingElse();
}

在用户界面中:

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


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

解决方案

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

这篇关于WPF DataGrid列:如何管理值更改的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆