在 WPF 中使用字符串命名变量/对象

Hui Liu-MSFT 48,711 信誉分 Microsoft 外部员工
2024-06-20T07:28:41.8733333+00:00

你好

我对编程相当陌生,只是想知道是否可以使用字符串在 WPF 中动态命名新变量或对象。我在 Windows 窗体上看到您可以使用 me.controls 来命名事物,但这不包含在 WPF 中,我无法找到明确的答案。

这方面的一个例子是:

for i = 0 to 10 dim (“变量名称” + i ) 作为下一个新矩形

我问的唯一原因是我想制作一个矩形网格,可以在需要时动态创建,也可以在需要时进行编辑。

Note:此问题总结整理于:Naming a variable/object with a string in WPF

开发人员技术 | Windows Presentation Foundation
0 个注释 无注释

1 个答案

排序依据: 非常有帮助
  1. 匿名
    2024-06-20T09:46:43.21+00:00

    可以使用字符串在 WPF 中动态命名新变量或对象。要动态创建和编辑 Grid,可以参考以下示例 xaml 的代码:

    <StackPanel>  
            <ScrollViewer Height="300">  
                <ItemsControl Name="itemsControl" Background="Pink" ItemsSource="{Binding GridCollection}">  
      
                </ItemsControl>  
            </ScrollViewer>  
            <TextBox Name="tb" Text="{Binding Path=Num}" Width="200" Background="LightGreen"  HorizontalAlignment="Right"/>  
            <Button Click="Button_Click" Width="200" HorizontalAlignment="Right"  >create</Button>  
        </StackPanel>  
    

    xaml.cs代码:

    using System.Collections.ObjectModel;  
    using System.ComponentModel;  
    using System.Windows;  
    using System.Windows.Controls;  
    using System.Windows.Data;  
    using System.Windows.Media;  
    namespace GridOfRectangles  
    {  
      public partial class MainWindow : Window,INotifyPropertyChanged  
      {  
        public MainWindow()  
        {  
          InitializeComponent();  
          tb.DataContext = this;  
        }  
        private string num;  
        public string Num  
        {  
          get { return num; }  
          set  
          {  
            if (value != num)  
            {  
              num = value;  
              OnPropertyChanged("Num");  
            }  
          }  
        }  
        private ObservableCollection<Grid> GridCollection { get; set; }  
        private void CreateGridDynamic ()  
        {  
          GridCollection = new ObservableCollection<Grid>();  
          for (int i = 0; i < 10; i++)  
          {  
            Grid dynamicGrid = new Grid();  
              
            var myBinding = new Binding("")  
            {  
              Source = int.Parse(Num)  
            };  
            dynamicGrid.SetBinding(Grid.WidthProperty, myBinding);  
      
            dynamicGrid.Height = 100;  
            dynamicGrid.Background = new SolidColorBrush(Colors.AliceBlue);  
            ColumnDefinition co1 = new ColumnDefinition();  
            ColumnDefinition co2 = new ColumnDefinition();  
            dynamicGrid.ColumnDefinitions.Add(co1);  
            dynamicGrid.ColumnDefinitions.Add(co2);  
            RowDefinition gridRow1 = new RowDefinition();  
            gridRow1.Height = new GridLength(45);  
            RowDefinition gridRow2 = new RowDefinition();  
            gridRow2.Height = new GridLength(45);  
            dynamicGrid.RowDefinitions.Add(gridRow1);  
            dynamicGrid.RowDefinitions.Add(gridRow2);  
      
            TextBox textBox = new TextBox();  
            textBox.Text = i.ToString();  
            textBox.FontWeight = FontWeights.Bold;  
            textBox.Foreground = new SolidColorBrush(Colors.Green);  
            Grid.SetRow(textBox, 0);  
            Grid.SetColumn(textBox, 0);  
            dynamicGrid.Children.Add(textBox);  
      
            GridCollection.Add(dynamicGrid);  
          }  
        }  
        private void Button_Click(object sender, RoutedEventArgs e)  
        {  
          CreateGridDynamic();  
           
          itemsControl.ItemsSource = GridCollection;  
        }  
        public event PropertyChangedEventHandler PropertyChanged;  
        protected void OnPropertyChanged(string propertyName)  
        {  
          PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));  
        }  
      }  
    }  
    

    结果图片: 136573-3.gif


    如果回复有帮助,请点击“接受答案”并点赞。

    注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。

    0 个注释 无注释

你的答案

提问者可以将答案标记为“已接受”,审查方可以将答案标记为“已推荐”,这有助于用户了解答案是否解决了提问者的问题。