question

KathrynPorter-1902 avatar image
0 Votes"
KathrynPorter-1902 asked KathrynPorter-1902 commented

Naming a variable/object with a string in WPF

Hello

I'm fairly new to programming and was just wondering if it was possible to use a string to dynamically name a new variable or object in WPF. I have seen on windows forms you could use me.controls to name things but this is not included on WPF and I haven't been able to find a definitive answer.

an example of this would be:

for i = 0 to 10
dim ("variable name " + i ) as new rectangle
next

The only reason I ask is I want to make a grid of rectangles that can be dynamically created when needed and that can be edited when needed too.

Thank you

windows-wpf
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

HuiLiu-MSFT avatar image
1 Vote"
HuiLiu-MSFT answered KathrynPorter-1902 commented

You could use strings to dynamically name new variables or objects in WPF. For dynamically creating and editing Grid, you can refer to the following example
The code of 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>

The code of 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));
     }
   }
 }

The picture of result:
136573-3.gif


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


3.gif (65.4 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for the help

0 Votes 0 ·