WPF DataGrid列:如何管理值更改的事件 [英] WPF DataGrid columns: how to manage event of value changing
问题描述
< DataGrid x:Name =FixedPositionDataGrid HorizontalAlignment =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)。不需要插入的值,我只需要在其中一个值更改时引发事件。 编辑: 我如何修改它以获得预期的结果? 谢谢 你似乎这个问题从WinForms的角度来看。在WPF中,我们通常更喜欢操作数据对象而不是UI对象。你说你的项目没有一个 如果您的数据没有数据类型类,那么我建议您创建一个。 执行此操作并将您的集合属性设置为 在视图模型中: 在视图模型构造函数中: 在视图模型中: 在用户界面中: In my WPF C# project, I have a Datagrid as follows: 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: 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 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 After doing this and setting your collection property as the In the view model: In the view model constructor: In the view model: In the UI:
这篇关于WPF DataGrid列:如何管理值更改的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
我该如何执行?我需要定义事件方法的方式,我可以在其中定义一些操作。
我已经读过这个当wpf datagrid的单元格中的值使用MVVM更改时,如何引发事件?但是我没有将可观察的集合链接到datagrid。 / p>
Datagrid ItemSource与以下对象链接:
public class FixedPosition
{
[XmlAttribute]
public int index {get;组; }
public int percentage {get;组; }
public HorizontalAlignment alignment {get;组; }
}
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}... />
<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>
public class FixedPosition
{
[XmlAttribute]
public int index { get; set; }
public int percentage { get; set; }
public HorizontalAlignment alignment { get; set; }
}
ObservableCollection<T>
for your items, but I would recommend that you use one. INotifyPropertyChanged
interface in it. 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"); }
}
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}" ... />