Changing ContentTemplate based on ListBox selection

前端 未结 1 2081
無奈伤痛
無奈伤痛 2021-02-10 21:17

I have a Listbox and a Border in a StackPanel similar to the following:


      
          

        
1条回答
  •  臣服心动
    2021-02-10 21:55

    You can toggle the ContentTemplate using a DataTrigger as follows.
    Note, that I am binding an ObservableCollection to a simple object (Thing) with one property called Name, and am I binding the Content of the ContentControl to the SelectedItem in the ListBox using a ViewModel.

    
        
            
    
            
                
                    

    Here is the Thing class:

    public class Thing
    {
      public Thing(String name)
      {
         this.Name = name;
      }
    
      public String Name { get; set; }
    
      public static ObservableCollection GetThingList()
      {
         return new ObservableCollection(new Thing[3] {
                new Thing("People"), 
                new Thing("Animals"),
                new Thing("Cars")
            });
      }
    }
    

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