简体   繁体   中英

Bind Column value to dynamically created button in wpf?

I m developing a WPF application where in my database there are two columns(product_id,product_name). and want to add button dynamically on basis of number of product in database table.so bind this two columns with dynamically created button because i want to perform some action on click event on basis of product_id.can you help me to how to bind this two column with button or how extend button base class to solve this issue?

Please learn MVVM before writing single line of code in wpf.

For now on you can achieve what you wanted in below crude manner:

Model:

public class Product
{
   public ProductID{get;set;}
   public Productname{get;set;}
}

Code behind

public void Populate()
{
    IList<Product> products = LoadFromDatabase(); // Function to load database records as Product class instances.
    myListBox.ItemsSource = products;
}

View:

 <ListBox>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Button Content="{Binding ProductName}"></Button>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM