【问题标题】:I have a string of the name of a variable I want to change我有一个要更改的变量名称的字符串
【发布时间】:2020-11-29 11:21:32
【问题描述】:

这是对我正在尝试做的事情的过度简化,但我相信答案会让我继续前进......

我有几个这样绑定的字符串:

private string _Box1 = "";
public string Box1
{
    get { return _Box1; }
    set
    {
        _Box1 = value;
        OnPropertyChanged();
    }
}

我有几个盒子... Box1, Box2, Box3... 等等。
我可以简单地更改 Box1 的值: Box1 = "whatever";

如果我在循环中有一个具有正确名称的字符串,我如何遍历框变量名称(而不是控件)...

strining boxvariablename = "Box";
for(int i = 1; i < 100; i++)
{
     boxvariablename += i;
     ChangeVaribleByTheSameNameAsThisString(boxvariablename) = "This new string value"
}

【问题讨论】:

  • 为什么你不简单地使用像 List&lt;string&gt; 这样可以通过索引访问的集合,或者如果你真的需要通过名称访问它们,请使用 Dictionary&lt;string, string&gt;
  • 看看 ItemsControl。将其 ItemsSource 属性绑定到字符串集合,并将适当的 UI 元素放入其 ItemTemplate 以显示字符串项。

标签: c# wpf variables binding


【解决方案1】:

您可以使用反射。假设更新值的代码将在同一个类中,在一个实例方法下:

string boxVariablePrefix = "Box";
for (int i = 1; i < 100; i++)
{
     string boxVariableName = boxVariablePrefix + i;
     this.GetType().GetProperty(boxVariableName).SetValue(this, "This new string value", null);
}

【讨论】:

    【解决方案2】:

    根据您的情况,解决方案会有所不同。
    所有解决方案都受益于将实际数据存储在集合中。您可以遍历此集合以操作数据项。

    也可以使用迭代解决方案根据类的名称修改类的属性,例如使用Dictionary 或使用反射。由于这似乎不适合解决您的问题,因此我不会显示此内容。

    备注:您的示例在索引“1”处开始 for 循环。要从头到尾枚举集合,索引必须从“0”开始。否则你会跳过项目。索引始终从零开始。

    ViewModel.cs

    class ViewModel : INotifyPropertyChanged
    {
      public ObservableCollection<string> BoxValues { get; }
    
      public ViewModel() 
      {
        this.BoxValues = new ObservableCollection<string> 
        {
          "Box1 Value",
          "Box2 Value",
          "Box3 Value"
        }
      }
    
      private void ManipulateBoxValues()
      {
        for(int i = 0; i < 100; i++)
        {
          this.BoxValues[i] = "This new string value"
        }
      }
    }
    

    MainWindow.xaml

    如果绑定到数据的控件分散在视图中(未分组),您可以使用索引器绑定到集合:

    <Window>
      <Window.DataContext>
        <ViewModel />
      </Window.DatContext>
    
      <StackPanel>
        <StackPanel Orientation="Horizontal">
          <TextBox Text="{Binding BoxValues[0]}" />
          <TextBox Text="{Binding BoxValues[1]}" />
        </StackPanel>
    
        <TextBox Text="{Binding BoxValues[2]}" />
      </StackPanel>
    </Window>
    

    如果将这些项目组合在一起,您应该使用ItemsControl(如ListBox)并定义一个可选的DataTemplate
    如果项目的类型为string,则只需要定义DataTemplate,以便在显示纯文本值之外增强项目布局(例如,当您需要将文本显示为TextBox 时)。默认情况下,ItemsControl 将创建一个TextBlock 来显示项目:

    <Window xmlns:system="clr-namespace:System;assembly=mscorlib">
      <Window.DataContext>
        <ViewModel />
      </Window.DatContext>
    
      <ListBox ItemsSource="{Binding BoxValues}">
    
        <!-- Optional. The DataContext of the template is the item itself. -->
        <ListBox.ItemTemplate>
          <DataTemplate DataType="system:String">
            <TextBox Text="{Binding} />
          </DataTemplate>
        </ListBox.ItemTemplate>
      </ListBox>
    </Window>
    

    【讨论】:

      猜你喜欢
      • 2012-09-05
      • 1970-01-01
      • 2012-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多