0

I created an example control that I want to copy as many times as many I will set in code. I want to duplicate entire <ToggleButton> control.

XAML:

    <WrapPanel Name="varom">
            <ToggleButton Margin="10">
                <StackPanel Orientation="Horizontal">
                    <Label Content="Stop sign" />
                    <Image Width="16" Source="{Binding appbar_stop}" />
                </StackPanel>
            </ToggleButton>

            <ToggleButton Margin="10">
                <StackPanel Orientation="Horizontal">
                    <Label Content="Stop sign" />
                    <Image Width="16" Source="{Binding appbar_stop}" />
                </StackPanel>
            </ToggleButton>

        </WrapPanel>

Now I copied one time <ToggleButton> manually, but if I would have just one <ToggleButton> and I want to get second without copying xaml code...

Is it possible to duplicate(copy) <ToggleButton> control using code?

C#:

namespace WpfApplication3
{
    public partial class MainWindow : MetroWindow
    {
        public const int maxButtons = 4;   // number of copies for example

        public MainWindow()
        {
            InitializeComponent();
            // code add here for example :)
        }

    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
armandasalmd
  • 169
  • 3
  • 10

1 Answers1

0

Models:

public class ButtonViewModel
{
    public string Caption { get; set; } 
}

public class ViewModel
{
    public ViewModel()
    {
       Buttons = new ObservableCollection<ButtonViewModel>
       {
           new ButtonViewModel { Caption = "Button 1" },
           new ButtonViewModel { Caption = "Button 2" },
           new ButtonViewModel { Caption = "Button 3" },
       };
    }

    public ObservableCollection<ButtonViewModel> Buttons { get; }  
}

XAML:

<ItemsControl ItemsSource="{Binding Buttons}" >
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <WrapPanel Orientation="Horizontal"/>
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <ToggleButton Margin="10">
        <StackPanel Orientation="Horizontal">
          <Label Content="{Binding Caption}" />
          <Image Width="16"/>
        </StackPanel>
      </ToggleButton>
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>
I.J.S.Gaus
  • 36
  • 1