您尝试做的事情没有多大意义,尤其是从维护的角度来看。您显然有 250 个要列出的变量。如果这个列表被缩短或扩展,你现在有 2389 个变量(夸大了,但只是为了重点)。你会为了保持头发而拔掉头发。
相反,您应该更多地了解类结构、更多可能的数据库,并尝试找到一个共同的模式。您拥有的每个“事物”都是基于整数的项目。但是每一个“东西”都有对应的用途,或者描述比如你的IA、IB、IC等等。
我建议您考虑以下内容。我已经制作了一个带有清单的枚举器。下面的示例,但我只有 IA-IG 的项目。出于示例目的,我还添加了与其中两个相关的描述。
public enum my250Things
{
IA,
IB,
[Description("this is item C")]
IC,
ID,
IE,
[Description("testing F description")]
IF,
IG
}
所以,现在在您的代码中,如果我有一个基于“my250Things”类型的变量,我可以通过它的枚举来引用它以了解它代表什么。
回到您的数据网格。由于要呈现的所有记录各占一行,并显示相应的变量名称上下文(IA、IB 等)及其包含的值,因此我创建了一个类。这个类实际上有 3 个属性。一个显示值,一个显示 int,以及它创建的原始枚举值。下面。
public class YourCommonThings : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _yourShowValue;
public string YourShowValue
{
get { return _yourShowValue; }
set { _yourShowValue = value;
NotifyPropertyChanged(); }
}
private int _yourIntValue;
public int YourIntValue
{
get { return _yourIntValue; }
set
{
_yourIntValue = value;
NotifyPropertyChanged();
}
}
private my250Things _theOriginalEnum;
public my250Things TheOriginalEnum
{
get { return _theOriginalEnum; }
set
{
_theOriginalEnum = value;
NotifyPropertyChanged();
}
}
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
现在,我想填充我想呈现给用户的内容列表。由于枚举让我知道我正在尝试跟踪的“事物”,因此我可以使用它来填充列表,以及适用的描述。如果没有,它默认使用枚举本身的字符串表示。我为演示创建了一个简单的窗口“Stack1”,创建了一个用于将数据列表绑定到并基于枚举自动填充的公共属性。
您需要在窗口的 .cs 文件顶部使用这些“使用”引用
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Windows;
这是窗口代码隐藏中的代码
public partial class Stack1 : Window
{
// first, making a public property as a list of the things I want to present
public List<YourCommonThings> BindToThisListOfThings { get; set; }
// the name of my window is called "Stack1" and this is the constructor of the window
public Stack1()
{
// start by declaring a new list of things based on the class
BindToThisListOfThings = new List<YourCommonThings>();
// Now, we can dynamically create a list of all the "things" you are trying to present
// into your data grid. In this case, I am going through an enumerator list, but could
// also be done if the list of things was stored in a database (for example) and you
// queried from that, but that is a different question / option.
foreach(my250Things oneThing in typeof( my250Things ).GetEnumValues() )
{
// creating one instance of whatever your enum underlying thing is and
// default setting the properties within the class instance for the parts
// that are within the { }
// the ".ToString()" portion will get your "IA","IB", ...
// I can also add in to store the original enum value while I'm at it,
// even though not showing to the end user in the screen.
var justOne = new YourCommonThings
{ YourShowValue = oneThing.ToString(),
YourIntValue = 0,
TheOriginalEnum = oneThing };
// and you can optionally just for sake of example purposes associate a description
// to an enum value and pull THAT instead...
string description = GetEnumDescription(oneThing);
if (!string.IsNullOrWhiteSpace(description))
justOne.YourShowValue = description;
// Now, add to your list
BindToThisListOfThings.Add(justOne);
}
// Now, you have your list prepared and ready to go for binding to
DataContext = this;
// and finish initializing the rest of the form.
InitializeComponent();
}
// this static method is used to get the "description" component
// out of the enumeration declaration IF such a description is declared.
public static string GetEnumDescription(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];
if (attributes != null && attributes.Any())
return attributes.First().Description;
return value.ToString();
}
}
最后,在表单的 xaml 中,我将数据网格绑定到从枚举中准备的事物列表。
<DataGrid AutoGenerateColumns="False" VerticalContentAlignment="Stretch"
ItemsSource="{Binding BindToThisListOfThings, NotifyOnSourceUpdated=True}">
<DataGrid.Columns>
<DataGridTextColumn Header="Argument Name" Binding="{Binding YourShowValue}" Width="125" />
<DataGridTextColumn Header="Argument Value" Binding="{Binding YourIntValue}" Width="125" />
</DataGrid.Columns>
</DataGrid>
现在,在表单/窗口中,或者在任何您定义了 MVVM 模式的地方(此时尚存疑问),但无论如何,无论您打算保存数据,您都可以循环浏览事物列表,并获取其各自的东西和描述要保存在您计划的任何地方,但您的问题中没有披露以进一步应用。
public void SaveData()
{
foreach( var oneThing in BindToThisListOfThings )
{
// Here, you can refer to
// oneThing.YourShowValue
// oneThing.YourIntValue
// oneThing.TheOriginalEnum
}
}
祝你好运。