我有一个班级Feature
:
public class Feature
{
public string Name { get; set; }
public string DisplayName { get; set; }
public List<Feature> SubFeatures { get; set; }
public bool Enabled { get; set; }
public string[] DependsOn { get; set; }
public bool IsRole { get; set; }
public bool ManuallySelected { get; set; }
}
DependsOn
包含其所依赖的任何其他Name
的{{1}}。
Features
只能拥有一个父母(不会直接在Feature
级别看到,以找出任何给定Feature
的父级需要找出哪个Feature
Feature
1}}将子Feature
列为SubFeature
)。
然后我按住List<Feature>
包含名为myFeatures
的var中的所有可能功能。
然后,我在Feature
(TreeListView
的一部分)中向用户显示这些ObjectListView
。
到目前为止,这么好。问题是我希望确保当用户选择Feature
时,Feature
的父项和Feature
的父项(以及父项)依赖关系,以及依赖关系和依赖关系的依赖关系依赖关系......等等也被选中。
我还确定用户选择了哪个Features
用户(使用ManuallySelected
属性),这样如果他们取消选择任何Feature
,任何父母和依赖关系(以及子依赖关系及其父母等)除非被手动选择的任何其他Feature
需要,否则也未被选中。
这是我的代码,当用户点击CheckBox
的{{1}}时...
Feature
这些是处理所有递归的辅助函数:
treeListView1.CheckStatePutter = (rowObject, value) =>
{
if (rowObject is Feature feature)
{
feature.Enabled = value == CheckState.Checked;
feature.ManuallySelected = feature.Enabled;
var flatFeatures = FlattenFeatures(_thisOperatingSystem.Features).ToList();
foreach (var nonSelectedFeature in flatFeatures.Where(x => !x.ManuallySelected))
{
nonSelectedFeature.Enabled = false;
}
foreach (var selectedFeature in flatFeatures.Where(x => x.ManuallySelected))
{
foreach (var subFeature in GetFeatureDependencies(flatFeatures, selectedFeature))
{
subFeature.Enabled = true;
}
}
treeListView1.Refresh();
}
return value;
};
我觉得在某个地方我做的比这应该复杂得多,但却无法想出改进它的方法。据我所知,它似乎正常工作,但我不确定没有错误和/或多次没有返回相同的public IEnumerable<Feature> GetFeatureDependencies(List<Feature> flatFeatures, Feature feature)
{
foreach (var parent in GetFeatureParents(flatFeatures, feature).Where(x => x != null))
{
yield return parent;
foreach (var parentDependency in GetFeatureDependencies(flatFeatures, parent))
{
yield return parentDependency;
}
}
foreach (var dependencyName in feature.DependsOn)
{
foreach (var dependency in flatFeatures.Where(x => x.Name == dependencyName))
{
yield return dependency;
foreach (var parent in GetFeatureParents(flatFeatures, dependency).Where(x => x != null))
{
yield return parent;
foreach (var parentDependency in GetFeatureDependencies(flatFeatures, parent))
{
yield return parentDependency;
}
}
foreach (var subDependency in GetFeatureDependencies(flatFeatures, dependency))
{
yield return subDependency;
foreach (var parent in GetFeatureParents(flatFeatures, subDependency).Where(x => x != null))
{
yield return parent;
foreach (var parentDependency in GetFeatureDependencies(flatFeatures, parent))
{
yield return parentDependency;
}
}
}
}
}
}
public IEnumerable<Feature> GetFeatureParents(List<Feature> flatFeatures, Feature feature)
{
var parent = flatFeatures.FirstOrDefault(x => x.SubFeatures.Contains(feature));
yield return parent;
if (parent == null)
{
yield break;
}
foreach (var parentsParent in GetFeatureParents(flatFeatures, parent))
{
yield return parentsParent;
}
}
public IEnumerable<Feature> FlattenFeatures(IEnumerable<Feature> features)
{
foreach (var feature in features)
{
yield return feature;
foreach (var child in FlattenFeatures(feature.SubFeatures))
{
yield return child;
}
}
}
。< / p>
任何帮助将不胜感激