Specific control for a specific DataType

前端 未结 1 1620
长发绾君心
长发绾君心 2020-12-20 02:56

I have a List which contains different types of objects:

    List myList = new List();
    DateTime date = DateTime.Now;
    myLi         


        
      
      
      
1条回答
  •  没有蜡笔的小新
    2020-12-20 03:08

    
      
    
        
        
          
        
    
        
        
          
            
              
            
          
          
        
    
    
        
        
          
        
      
      
    
    

    Code Behind:

     public partial class DataTemplates : Window
        {
            public DataTemplates()
            {
                InitializeComponent();
    
                var myList = new List();
                myList.Add(DateTime.Now);
                myList.Add(50);
                myList.Add("Hello World");
    
                DataContext = myList;
            }
        }
    
    
    

    Result:

    enter image description here

    As you can see, there's no reason at all to use code to manipulate UI elements in WPF (except some very very specific cases)

    Edit:

    Note that you don't usually create a DataTemplate for classes inside the System namespace (such as System.String. This is only to give you an example. If you really needed this you would probably have to create a ViewModel for each type.

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