Dynamically add multiple buttons to wpf window?

前端 未结 3 2028
野的像风
野的像风 2020-12-01 04:17

how would i add multiple buttons to a window in c#? here\'s what i need to do... i\'m getting multiple user values from a dictionary (within reason, only @ 5-6 values). for

3条回答
  •  孤街浪徒
    2020-12-01 04:26

    I would encapsulate the whole thing, there normally should be no point in naming the button. Something like this:

    public class SomeDataModel
    {
        public string Content { get; set; }
    
        public ICommand Command { get; set; }
    
        public SomeDataModel(string content, ICommand command)
        {
            Content = content;
            Command = command;
        }
    }
    

    Then you can create models and put them into a bindable collection:

    private readonly ObservableCollection _MyData = new ObservableCollection();
    public ObservableCollection MyData { get { return _MyData; } }
    

    Then you just need to add and remove items from that and create buttons on the fly:

    
        
            
                

    For more info see relevant articles on MSDN:

    Data Binding Overview
    Commanding Overview
    Data Templating Overview

提交回复
热议问题