How to create an array of buttons in WPF?

前端 未结 1 451
粉色の甜心
粉色の甜心 2021-02-15 13:24

I can create an array of buttons in Windows Form but how can i do that in WPF(xaml) ? thanks in advance!

1条回答
  •  礼貌的吻别
    2021-02-15 14:16

    You can't do it directly in XAML (though you can do it in code, in exactly the same way as in Windows Forms). What you can do instead is use data binding and ItemsControl to create the buttons for you. You don't say what you need the control array for, but suppose you want a button for each Person in a collection:

    // Code behind
    public Window1()
    {
      var people = new ObservableCollection();
      // Populate people
      DataContext = people;
    }
    
    // XAML
    
      
        
          

    You can actually set up the whole thing in XAML using the ObjectDataProvider and CollectionViewSource, but this should be enough to get you started. And obviously the source can be something other than business data depending on what you need the "array" for.

    0 讨论(0)
提交回复
热议问题