-3

I am having a set of buttons on window. when I clicked on that buttons I want to add Different controls dynamically. consider I am having two buttons 1> AddTextBox 2>AddButton

When I click on AddTextButton the TextBox should be added to window when I click on AddButton the Button should be added

  • *"through xaml"* ? Dynamically usually means programmatically (see [this answer](https://stackoverflow.com/a/7885604/1997232)). – Sinatr Sep 29 '17 at 10:38

2 Answers2

1

You can add like below code snippet,

private void AddButtonClick(object sender, RoutedEventArgs e)
    {
        var rowCount = this.grid.RowDefinitions.Count;
        this.grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
        var button = new Button() { Content = "Button1", Height = 20, Width = 50 };
        Grid.SetRow(button, rowCount + 1);
        this.grid.Children.Add(button);
    }
Smirti
  • 305
  • 2
  • 12
0

If you mean you want to dynamically add buttons via binding the following approach may help you.

At first you add a ItemsControl section to your xaml defining an ItemTemplate.

<ItemsControl ItemsSource="{Binding DynamicControlObjects}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBox Text="{Binding Text}"
                     ToolTip="{Binding Tooltip}"
                     IsEnabled="{Binding IsEnabled}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

The DynamicControlObjects is just a simple IList<T> where T is a class containing the properties you want to bind.

For example what I am using in this case is my class DynamicControlHelper:

// ...

using Microsoft.Practices.Prism.ViewModel;

// ...

public class DynamicControlHelper : NotificationObject
{
    #region Backing Fields

    private string _text;
    private bool _isEnabled;
    private string _tooltip;

    #endregion Backing Fields

    #region Properties

    public string Text
    {
        get
        {
            return this._text;
        }

        set
        {
            if (!string.Equals(this._text, value))
            {
                this._text = value;
                this.RaisePropertyChanged(nameof(this.Text));
            }
        }
    }

    public bool IsEnabled
    {
        get
        {
            return this._isEnabled;
        }

        set
        {
            if (this._isEnabled != value)
            {
                this._isEnabled = value;
                this.RaisePropertyChanged(nameof(IsEnabled));
            }
        }
    }

    // ...

    public string Tooltip
    {
        get
        {
            return this._tooltip;
        }

        set
        {
            if (!string.Equals(this._tooltip, value))
            {
                this._tooltip = value;
                this.RaisePropertyChanged(nameof(this.Tooltip));
            }
        }
    }

    #endregion Properties
}

When I needed this approach the first time. The answer from H.B. leads me to my solution.

Martin Backasch
  • 1,829
  • 3
  • 20
  • 30