Daniel
Daniel

Reputation: 242

How to address a UI Element I created dynamically in my code?

I'm currently working at a project where I get parameters sent via RS232. My program will be running on a Raspberry Pi and will use the Win10 IoT platform.

To display the parameters I recieve I create a Grid dynamically from my code behind file. (This is already working.)

After I get the parameters the device will send me a list with min. and max. values. Using this list as a reference I create a slider, toggleswitch or textbox.

if ((maximum - minimum) > 1)
        {
            Slider ParameterSlider = new Slider();

            ParameterSlider.Name = "Slider" + CountControls;
            ParameterSlider.Width = 200;
            ParameterSlider.ValueChanged += new RangeBaseValueChangedEventHandler(ParameterSlider1_ValueChanged);
            ParameterSlider.Minimum = minimum;
            ParameterSlider.Maximum = maximum;
            ParameterSlider.Value = 0;
            ParameterSlider.VerticalAlignment = VerticalAlignment.Center;
            ParameterSlider.HorizontalAlignment = HorizontalAlignment.Left;

            SettingsGrid.Children.Add(ParameterSlider);
            Grid.SetColumn(ParameterSlider, 2);
            Grid.SetRow(ParameterSlider, CountControls);
        }

"minimum" and "maximum" are the variables i get from the serial device.

As you can see I set the name of the slider element to "Slider" and add the position of the parameter as a number after it.

"CountControls" is a variable I use to create the Grid in the right order. It just counts up from 0 to 100 when I recieve a serial input.

For every element (Slider, Toogleswitch, TextBox) I create a TextBox which displays the value which the element currently has and adds a unit description (mm, m, kg, s) which I will also recieve from a list the serial device can send to me.

Everything is working so far but I have a problem adding the value box. Since I create the name of the elements dynamically I have no way to reference them in my code.

This is how I create the TextBox:

        TextBlock ParaValue = new TextBlock();

        Thickness ValueMargin = ParaValue.Margin;
        ValueMargin.Left = 10;
        ValueMargin.Top = 5;


        ParaValue.Name = "Value" + CountControls;
        ParaValue.FontSize = 20;
        ParaValue.Text = String.Empty + UnitBlock.Units[CountControls];
        ParaValue.VerticalAlignment = VerticalAlignment.Top;
        ParaValue.Margin = ValueMargin;

        SettingsGrid.Children.Add(ParaValue);
        Grid.SetColumn(ParaValue, 3);
        Grid.SetRow(ParaValue, CountControls);

The "ParaValue.Text" is currently containing a "String.Empty" and I want this to be replaced with my value from the UI elements.

How can I possibly achieve this?

Upvotes: 2

Views: 968

Answers (3)

Rita Han
Rita Han

Reputation: 9700

For referencing elements created dynamically, you can find it like this:

        foreach (UIElement element in SettingsGrid.Children)
        {
            System.Diagnostics.Debug.WriteLine(element.GetType());
            if (element.GetType().Name is "Slider")
            {
                var elementNew = element as Slider;
                ParaValue.Text = elementNew.Value.ToString();
            }

        }

Upvotes: 0

Milan
Milan

Reputation: 616

Binding binding = new Binding();
binding.Path = new PropertyPath("Value");
binding.Source = ParameterSlider; 
BindingOperations.SetBinding(ParaValue, TextBlock.TextProperty, binding);

now your textbox.text is bound to Value of the slider and auto-updates it. cool?

Upvotes: 1

A. Milto
A. Milto

Reputation: 2383

Here you assign unique names to your dynamically created controls:

ParameterSlider.Name = "Slider" + CountControls;
...
ParaValue.Name = "Value" + CountControls;

You can always find a UI element by its name later:

TextBlock dynamicallyCreatedTextBlock = SettingsGrid.FindName("Value129") as TextBlock;
dynamicallyCreatedTextBlock.Text = "Got it";

Upvotes: 0

Related Questions