Skip to content

Commit f1532ba

Browse files
committed
feat: 源字段默认自动更新目标字段
1 parent 9452f69 commit f1532ba

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

Models/FieldMapping.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.ComponentModel;
2+
using System.Runtime.CompilerServices;
23

34
namespace ExcelMatcher.Models;
45

@@ -18,8 +19,10 @@ public string SourceField
1819
get => _sourceField;
1920
set
2021
{
21-
_sourceField = value;
22-
OnPropertyChanged(nameof(SourceField));
22+
if (SetProperty(ref _sourceField, value))
23+
// 当源字段变更时,总是将目标字段更新为源字段的值
24+
if (!string.IsNullOrEmpty(value))
25+
TargetField = value;
2326
}
2427
}
2528

@@ -29,16 +32,22 @@ public string SourceField
2932
public string TargetField
3033
{
3134
get => _targetField;
32-
set
33-
{
34-
_targetField = value;
35-
OnPropertyChanged(nameof(TargetField));
36-
}
35+
set => SetProperty(ref _targetField, value);
3736
}
3837

3938
public event PropertyChangedEventHandler? PropertyChanged;
4039

41-
protected virtual void OnPropertyChanged(string propertyName)
40+
protected bool SetProperty<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
41+
{
42+
if (EqualityComparer<T>.Default.Equals(field, value))
43+
return false;
44+
45+
field = value;
46+
OnPropertyChanged(propertyName);
47+
return true;
48+
}
49+
50+
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
4251
{
4352
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
4453
}

ViewModels/MainViewModel.cs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -934,11 +934,31 @@ private void AddFieldMapping()
934934
{
935935
try
936936
{
937-
var mapping = new FieldMapping
937+
var mapping = new FieldMapping();
938+
939+
// 智能填充源字段:优先使用选中的辅助表匹配字段
940+
if (SelectedSecondaryMatchFields.Count > 0)
941+
{
942+
// 查找还未被映射的选中字段
943+
var unmappedFields = SelectedSecondaryMatchFields
944+
.Where(field => !FieldMappings.Any(fm => fm.SourceField == field))
945+
.ToList();
946+
947+
if (unmappedFields.Count > 0)
948+
mapping.SourceField = unmappedFields.First();
949+
// 目标字段会通过SourceField的setter自动填充
950+
else if (SelectedSecondaryMatchFields.Count > 0)
951+
// 如果所有选中字段都已映射,使用第一个选中字段
952+
mapping.SourceField = SelectedSecondaryMatchFields.First();
953+
}
954+
else if (SecondaryColumns.Count > 0)
938955
{
939-
SourceField = SecondaryColumns.FirstOrDefault() ?? string.Empty,
940-
TargetField = string.Empty
941-
};
956+
// 如果没有选中字段,使用第一个可用字段
957+
mapping.SourceField = SecondaryColumns.FirstOrDefault() ?? string.Empty;
958+
}
959+
960+
// 如果以上都没有设置源字段,则保持空值
961+
// 这样用户在下拉选择时会触发自动填充
942962

943963
// 监听属性变更
944964
if (mapping is INotifyPropertyChanged notifyMapping)
@@ -2835,11 +2855,12 @@ private void ApplyFieldSelections(Configuration config)
28352855
foreach (var mapping in config.FieldMappings)
28362856
if (!string.IsNullOrEmpty(mapping.SourceField) && SecondaryColumns.Contains(mapping.SourceField))
28372857
{
2838-
var newMapping = new FieldMapping
2839-
{
2840-
SourceField = mapping.SourceField,
2841-
TargetField = mapping.TargetField
2842-
};
2858+
var newMapping = new FieldMapping();
2859+
2860+
// 先设置目标字段,避免被源字段自动覆盖
2861+
newMapping.TargetField = mapping.TargetField;
2862+
// 然后设置源字段,如果目标字段为空,会被自动填充
2863+
newMapping.SourceField = mapping.SourceField;
28432864

28442865
// 监听属性变更
28452866
if (newMapping is INotifyPropertyChanged notifyMapping)

0 commit comments

Comments
 (0)