0

I am having problems with generating a click event for items inside an items control. I am new to xaml nad wpf. Can you the experts help me out. Below is the code that i have come up with but now having no clue on how to add a click event to for the generated items. Will very much appreciate your responses. Thank you for reading

<ItemsControl ItemsSource="{Binding Text, Source={StaticResource TextContainer}}">
            <!--text is an object bein made public from TextToDisplay. There can be many objects released ratger than one in this case-->
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel ItemWidth="100"
                               ItemHeight="100" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate DataType="{x:Type sys:String}">
                    <Border CornerRadius="100"
                            Background="BlueViolet">
                        <Button Margin="20"
                                   Content="{Binding}"
                                   HorizontalContentAlignment="Center" />
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
Chris W.
  • 22,835
  • 3
  • 60
  • 94
  • ................... – Dot Naughty May 11 '15 at 18:41
  • You're going to have to edit your question and post the code there. Comment code is utterly unreadable. – goobering May 11 '15 at 18:46
  • Maybe a `ListBox` or something with Item events already handled would be more suited for your purpose, otherwise yea you can put Button's or whatever you like in an ItemsControl, there's also other info available to point you in the right direction. – Chris W. May 11 '15 at 18:52
  • possible duplicate of [ItemsControl button click command](http://stackoverflow.com/questions/9716133/itemscontrol-button-click-command) – Chris W. May 11 '15 at 18:52

1 Answers1

0

probably the easiest thing you can do is create your own user control... make it a custom button and add click event to that control so everytime the listbox add your control it will already have a click_event built in.

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        // add your code here.
    }

then for the listbox do something like that...

    private void AddItemsButton_Click(object sender, RoutedEventArgs e)
    {

        ListBoxTest.Items.Add(new UserControl1());
    }

this adds a usercontrol button to the listbox everytime you click on a button titled "add items"

Dark Templar
  • 1,130
  • 8
  • 10