DataGrid WPF字段

时间:2017-01-13 23:49:05

标签: c# wpf datagrid field cell

我有一个包含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中。 我遍历集合以获取实例,并设置下一行的值。

如果任何字段得到更新,程序需要相应地更新。

现在我只能操作Beginning字段,但不能操作Length字段。

将起始字段视为文本文档中的一行,并将长度字段视为文本文档中的下一行,这两个字段必须完全分开并且永远不会相互重叠,我希望我在哪个方面有意义我想要完成我是一个相当新的编程和我的算法技能不是很有效。请帮助我,如果我说的话没有意义,请让我知道我会尽我所能详细说明。 有关详细信息,请参阅代码。

谢谢!!

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"); 
            }

        }
    }
}

1 个答案:

答案 0 :(得分:1)

您应该在视图模型类中处理此逻辑。

您可以处理添加到PropertyChanged的每个Field对象的ObservableCollection<Field>事件,并相应地重新计算“开始”和“长度”字段。请参考以下示例代码,它们可以为您提供这个想法:

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");
            }
        }
    }
}

enter image description here