数据网格 WPF 字段
DataGrid WPF Fields
我有一个包含 3 个 DataGridTextColumns 的数据网格
<DataGrid.Columns>
<DataGridTextColumn Header="Field Name" Binding="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn x:Name="BeginFields" Header="Begin" Binding="{Binding Begin, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn x:Name="LengthFields" Header="Length" Binding="{Binding Length, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataGrid.Columns>
所有三个 DataGridTextColumns 数据都添加到一个 ObservableCollection 中。
我遍历集合以获取一个实例,并为下一行设置值。
如果任何字段得到更新,程序需要相应地更新。
现在我只能操作开始字段,不能操作长度字段。
将起始字段视为文本文档中的一行,将长度字段视为文本文档中的下一行,这两个字段必须完全分开并且永远不会相互重叠,我希望我在我想要完成的事情我对编程还很陌生,而且我的算法技能并不强大。请帮助我,如果我说的话没有意义,请告诉我,我会尽我所能详细说明。
有关详细信息,请参阅代码。
谢谢!!
The DataGrid View
我现在将过去的代码
public MainWindow()
{
InitializeComponent();
dataGrid.ItemsSource = MWVM.FieldsCollection;
fileButton.Click += FileButton_Click;
loadPresetButton.Click += LoadPresetButton_Click;
savePresetButton.Click += SavePresetButton_Click;
clipboardButton.Click += ClipboardButton_Click;
saveButton.Click += SaveButton_Click;
****dataGrid.SelectedCellsChanged += ((o, e) =>
{
int track = 0;
foreach (Field f in MWVM.FieldsCollection)
{
track += f.Length;
f.Begin = track - f.Length;
}
}); ****
}
public class Field : ViewModelsBase
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
RaisePropertyChanged("Name");
}
}
private int _begin;
public int Begin
{
get
{
return _begin;
}
set
{
if (value != _begin)
{
_begin = value;
RaisePropertyChanged("Begin");
}
}
}
private int _length;
public int Length
{
get { return _length; }
set
{
if (value != _length)
{
_length = value;
RaisePropertyChanged("Length");
}
}
}
[JsonIgnore]
public string Blank { get; set; } = "";
}
public class MainWindowViewModel : ViewModelsBase
{
public ObservableCollection<Field> fieldsCollection;
public MainWindowViewModel()
{
_fields = CreateFieldsCollection();
}
private ObservableCollection<Field> CreateFieldsCollection()
{
fieldsCollection = new ObservableCollection<Field>();
return fieldsCollection;
}
ObservableCollection<Field> _fields;
public ObservableCollection<Field> FieldsCollection
{
get
{
return _fields;
}
set
{
if (_fields != value)
{
_fields = value;
RaisePropertyChanged("FieldsCollection");
}
}
}
}
您应该在视图模型中处理此逻辑class。
您可以处理添加到 ObservableCollection<Field>
的每个 Field
对象的 PropertyChanged
事件,并相应地重新计算 Begin 和 Length 字段。请参考以下示例代码,您应该可以从中得到启发:
public class MainWindowViewModel : ViewModelsBase
{
public MainWindowViewModel()
{
_fields = CreateFieldsCollection();
_fields.CollectionChanged += _fields_CollectionChanged;
}
private ObservableCollection<Field> CreateFieldsCollection()
{
ObservableCollection<Field> fieldsCollection = new ObservableCollection<Field>();
fieldsCollection.Add(new Field() { Begin = 0, Length = 5 });
fieldsCollection.Add(new Field() { Begin = 5, Length = 5 });
fieldsCollection.Add(new Field() { Begin = 10, Length = 5 });
fieldsCollection.Add(new Field() { Begin = 15, Length = 5 });
fieldsCollection.Add(new Field() { Begin = 20, Length = 5 });
foreach (Field field in fieldsCollection)
field.PropertyChanged += item_PropertyChanged;
return fieldsCollection;
}
private void _fields_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (object field in e.NewItems)
{
(field as INotifyPropertyChanged).PropertyChanged
+= new PropertyChangedEventHandler(item_PropertyChanged);
}
}
if (e.OldItems != null)
{
foreach (object country in e.OldItems)
{
(country as INotifyPropertyChanged).PropertyChanged
-= new PropertyChangedEventHandler(item_PropertyChanged);
}
}
}
private void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (_fields.Count < 2)
return;
switch(e.PropertyName)
{
case "Length":
//recalculate the begin of each field
RecalculateBegin();
break;
case "Begin":
Field modifiedField = sender as Field;
int indexOfModifiedField = FieldsCollection.IndexOf(modifiedField);
if(indexOfModifiedField > 0)
{
//recalculate the length of the previous field:
Field previousField = FieldsCollection[indexOfModifiedField - 1];
previousField.Length = modifiedField.Begin - previousField.Begin;
}
//...and the recalculate the begin of the rest of the fields:
RecalculateBegin();
break;
}
}
private void RecalculateBegin()
{
int length = _fields[0].Begin + _fields[0].Length;
for (int i = 1; i < FieldsCollection.Count; ++i)
{
FieldsCollection[i].Begin = length;
length += FieldsCollection[i].Length;
}
}
private ObservableCollection<Field> _fields;
public ObservableCollection<Field> FieldsCollection
{
get { return _fields; }
set
{
if (_fields != value)
{
_fields = value;
RaisePropertyChanged("FieldsCollection");
}
}
}
}
我有一个包含 3 个 DataGridTextColumns 的数据网格
<DataGrid.Columns>
<DataGridTextColumn Header="Field Name" Binding="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn x:Name="BeginFields" Header="Begin" Binding="{Binding Begin, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn x:Name="LengthFields" Header="Length" Binding="{Binding Length, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataGrid.Columns>
所有三个 DataGridTextColumns 数据都添加到一个 ObservableCollection 中。 我遍历集合以获取一个实例,并为下一行设置值。
如果任何字段得到更新,程序需要相应地更新。
现在我只能操作开始字段,不能操作长度字段。
将起始字段视为文本文档中的一行,将长度字段视为文本文档中的下一行,这两个字段必须完全分开并且永远不会相互重叠,我希望我在我想要完成的事情我对编程还很陌生,而且我的算法技能并不强大。请帮助我,如果我说的话没有意义,请告诉我,我会尽我所能详细说明。 有关详细信息,请参阅代码。
谢谢!!
The DataGrid View
我现在将过去的代码
public MainWindow()
{
InitializeComponent();
dataGrid.ItemsSource = MWVM.FieldsCollection;
fileButton.Click += FileButton_Click;
loadPresetButton.Click += LoadPresetButton_Click;
savePresetButton.Click += SavePresetButton_Click;
clipboardButton.Click += ClipboardButton_Click;
saveButton.Click += SaveButton_Click;
****dataGrid.SelectedCellsChanged += ((o, e) =>
{
int track = 0;
foreach (Field f in MWVM.FieldsCollection)
{
track += f.Length;
f.Begin = track - f.Length;
}
}); ****
}
public class Field : ViewModelsBase
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
RaisePropertyChanged("Name");
}
}
private int _begin;
public int Begin
{
get
{
return _begin;
}
set
{
if (value != _begin)
{
_begin = value;
RaisePropertyChanged("Begin");
}
}
}
private int _length;
public int Length
{
get { return _length; }
set
{
if (value != _length)
{
_length = value;
RaisePropertyChanged("Length");
}
}
}
[JsonIgnore]
public string Blank { get; set; } = "";
}
public class MainWindowViewModel : ViewModelsBase
{
public ObservableCollection<Field> fieldsCollection;
public MainWindowViewModel()
{
_fields = CreateFieldsCollection();
}
private ObservableCollection<Field> CreateFieldsCollection()
{
fieldsCollection = new ObservableCollection<Field>();
return fieldsCollection;
}
ObservableCollection<Field> _fields;
public ObservableCollection<Field> FieldsCollection
{
get
{
return _fields;
}
set
{
if (_fields != value)
{
_fields = value;
RaisePropertyChanged("FieldsCollection");
}
}
}
}
您应该在视图模型中处理此逻辑class。
您可以处理添加到 ObservableCollection<Field>
的每个 Field
对象的 PropertyChanged
事件,并相应地重新计算 Begin 和 Length 字段。请参考以下示例代码,您应该可以从中得到启发:
public class MainWindowViewModel : ViewModelsBase
{
public MainWindowViewModel()
{
_fields = CreateFieldsCollection();
_fields.CollectionChanged += _fields_CollectionChanged;
}
private ObservableCollection<Field> CreateFieldsCollection()
{
ObservableCollection<Field> fieldsCollection = new ObservableCollection<Field>();
fieldsCollection.Add(new Field() { Begin = 0, Length = 5 });
fieldsCollection.Add(new Field() { Begin = 5, Length = 5 });
fieldsCollection.Add(new Field() { Begin = 10, Length = 5 });
fieldsCollection.Add(new Field() { Begin = 15, Length = 5 });
fieldsCollection.Add(new Field() { Begin = 20, Length = 5 });
foreach (Field field in fieldsCollection)
field.PropertyChanged += item_PropertyChanged;
return fieldsCollection;
}
private void _fields_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (object field in e.NewItems)
{
(field as INotifyPropertyChanged).PropertyChanged
+= new PropertyChangedEventHandler(item_PropertyChanged);
}
}
if (e.OldItems != null)
{
foreach (object country in e.OldItems)
{
(country as INotifyPropertyChanged).PropertyChanged
-= new PropertyChangedEventHandler(item_PropertyChanged);
}
}
}
private void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (_fields.Count < 2)
return;
switch(e.PropertyName)
{
case "Length":
//recalculate the begin of each field
RecalculateBegin();
break;
case "Begin":
Field modifiedField = sender as Field;
int indexOfModifiedField = FieldsCollection.IndexOf(modifiedField);
if(indexOfModifiedField > 0)
{
//recalculate the length of the previous field:
Field previousField = FieldsCollection[indexOfModifiedField - 1];
previousField.Length = modifiedField.Begin - previousField.Begin;
}
//...and the recalculate the begin of the rest of the fields:
RecalculateBegin();
break;
}
}
private void RecalculateBegin()
{
int length = _fields[0].Begin + _fields[0].Length;
for (int i = 1; i < FieldsCollection.Count; ++i)
{
FieldsCollection[i].Begin = length;
length += FieldsCollection[i].Length;
}
}
private ObservableCollection<Field> _fields;
public ObservableCollection<Field> FieldsCollection
{
get { return _fields; }
set
{
if (_fields != value)
{
_fields = value;
RaisePropertyChanged("FieldsCollection");
}
}
}
}