0

I am reading a database file and based on the no. of entries output from the database to my query, I want to populate the buttons. And on clicking any of these buttons, I want to call an on click event for that entry in the database. How can I do that?

Template for Button:

<Window.Resources>
    <ControlTemplate TargetType="Button" x:Key="RoundBtn">
        <Border Name="roundBorder" CornerRadius="12.5" Height="25" Width="95" Margin="0" BorderBrush="Green" BorderThickness="0,0,0,0"
            Background="Green">
            <TextBlock Text="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="12,0,13,0" Foreground="White"
            />
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsEnabled" Value="false">
                <Setter TargetName="roundBorder" Property="Background" Value="Gray" />
                <Setter TargetName="roundBorder" Property="BorderBrush" Value="Gray" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</Window.Resources>

I need help in creating buttons and creating the on click event for calling it. I can handle the logic inside the on click event, but the problem is to identify which button was clicked on, so as to pass data for that particular button.

3
  • @ASh If you have any idea about it, Please do help! Commented Mar 30, 2018 at 11:17
  • take a look at my answer :) Commented Mar 30, 2018 at 11:23
  • 1
    You make a separate UserControl that has a button in it. That button uses the click event in the code behind of that UserControl. You then reference that UserControl in the DataTemplate. Commented Aug 28, 2018 at 15:39

2 Answers 2

1

Rather than creating a ControlTemplate,just create a Style.Then the code would be like :

 Button btn = new Button
 btn.Style= (Style)FindResource("RoundBtn")
 grid.Children.Add(btn);
 btn.click += new EventHandler(btn_click);

 private void btn_Click(object sender, RoutedEventArgs e)
{
 }
Sign up to request clarification or add additional context in comments.

2 Comments

How to relate this new button to a particular on click event?
re-check the ans and mark it as answer if it helped :)
1

The task of generating multiple controls for multiple items from some list is best solved using ItemsControl:

<ItemsControl Name="itemsList">
    <ItemsControl.ItemTemplate>
        <DataTemplate DataType="local:DataEntity">
            <Button Template="{StaticResource RoundBtn}" 
                    Content="{Binding Name}"
                    Click="ItemButtonClick"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

This ItemsControl named "itemsList" contains buttons with custom template. Each button will display one the Name of one item from list.

DataEntity is a class which contains values from db, e.g.

public class DataEntity
{
    public string Name { get; set; }
}

Items list is linked to ItemsControl via ItemsSource property (in my demo I'm doing it in window code-behind in constructor)

itemsList.ItemsSource = new ObservableCollection<DataEntity>
{
    new DataEntity { Name = "A" },
    new DataEntity { Name = "B" },
    new DataEntity { Name = "C" },
};

Buttons have click handler attached ("ItemButtonClick"). The clicked button is determined from sender argument:

private void ItemButtonClick(object sender, RoutedEventArgs e)
{
    var button = (Button)sender;
    var item = button.DataContext as DataEntity;

    MessageBox.Show("Clicked " + item.Name);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.