WPf 带滚动条WrapPanel 自动换行 和控件右键菜单

技能点包括 WPf 样式的引用 数据的验证和绑定 比较适合初学者

WPf 带滚动条WrapPanel 自动换行 和控件右键菜单_第1张图片

前台:

 
        
        
        
            
                
                    
                
            
        

        
            
                
                    
                        
                        
                        
                    

                
                
                
            
        
    
    
        
            
                
            

         
                
            
                
                    
                        
                    
                
            
        
   
    
        
    

后台:

   /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            load();
        }

        public void load() 
        {
            ObservableCollection ListMean = new ObservableCollection() 
            {
                 new Mean(){Text="001",Url="i"},
                 new Mean(){Text="002",Url="i"},
                 new Mean(){Text="003",Url="ii"},
                 new Mean(){Text="004",Url="i"},
                 new Mean(){Text="005",Url="i"},
                 new Mean(){Text="006",Url="ii"},
                 new Mean(){Text="007",Url="i"},
                 new Mean(){Text="008",Url="i"},
                 new Mean(){Text="009",Url="i"},
                 new Mean(){Text="010",Url="i"},
                 new Mean(){Text="011",Url="ii"},
                 new Mean(){Text="012",Url="i"},
                 new Mean(){Text="013",Url="i"},
                 new Mean(){Text="014",Url="i"},
                 new Mean(){Text="015",Url="i"},
                 new Mean(){Text="016",Url="i"},  
                 new Mean(){Text="017",Url="i"}
            };
            list.ItemsSource = ListMean;
        }
    }
    public class Mean : INotifyPropertyChanged
    {
        private string text;

        public string Text
        {
            get { return text; }
            set { text = value; OnPropertyChanged("Text"); }
        }
        private string url;

        public string Url
        {
            get { return url; }
            set { url = value; OnPropertyChanged("Url"); }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {

            if (PropertyChanged != null)
            {

                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

            }

        }
    }
    public class PathToSource:IValueConverter
    {
       public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
       {
         
           string url = string.Format(@"/Images/{0}.jpg", (string)value=="i"?"i":"ii");
           return new BitmapImage(new Uri(url,UriKind.Relative));
       }
       public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
       {
           throw new NotImplementedException();
       }
    }


你可能感兴趣的:(WPF)