WPF: Setting ListView View via DataTrigger

前端 未结 2 470
轻奢々
轻奢々 2020-12-17 07:15

I have a list view and 2 resources for display the list\'s view: BooksGridView & ImageDetailView.

The ViewModel has a string property named ViewMode, which conta

2条回答
  •  醉梦人生
    2020-12-17 07:43

    Based off the sample on MSDN, the following works at changing the view based on a change in the ViewModel. The only difference I can see with your code is the use of DynamicResource:

    
    
        
    
          
            
              
                
              
              
              
            
          
    
          
            
              
              
              
            
          
    
          
            
            
          
    
          
            
              
                
                
                
                
              
            
          
    
          
            
              
            
          
    
          
            
            
            
            
          
          
          
    
        
    
        
    
      
        
          
            
              
              
              
            
          
    
    
        
        
          CurrentView: 
        
        
          Right-click in the content window to change the view.
          
    
      
     
    

    Code behind file:

    using System;
    using System.Windows;
    using System.Windows.Controls;
    
    namespace SDKSample
    {
        /// 
        /// Interaction logic for Window1.xaml
        /// 
    
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
            }
    
            public MainViewModel ViewModel
            {
                get { return this.DataContext as MainViewModel; }
            }
    
            void SwitchViewMenu(object sender, RoutedEventArgs args)
            {
                MenuItem mi = (MenuItem)sender;
                ViewModel.ViewName = mi.Header.ToString();
            }
    
            private void Window_SourceInitialized(object sender, EventArgs e)
            {
                ViewModel.ViewName = "gridView";
            }
        }
    }
    

    And finally the ViewModel class:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.ComponentModel;
    
    namespace SDKSample
    {
        public class MainViewModel : INotifyPropertyChanged
        {
            public string ViewName
            {
                get { return viewName; }
                set
                {
                    if (viewName == value)
                        return;
    
                    viewName = value;
                    NotifyPropertyChanged("ViewName");
                }
            }
            private string viewName;
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            void NotifyPropertyChanged(string name)
            {
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    }
    

提交回复
热议问题