in WPF I need to have a collection of a collection of drawing objects

前端 未结 1 346
后悔当初
后悔当初 2021-01-24 18:52

I have a WPF project that draws several things in a panel. For the next release I need to add another type of thing to draw in addition to the existing things. Currently I hav

1条回答
  •  再見小時候
    2021-01-24 19:42

    Thank your for the improved code example. From that, it appears to me that you are going about your goal the wrong way entirely.

    That is, you are trying to have a single ItemsControl object render all of your lines. But that's not how your data model is organized. Your data model has two completely different kinds of objects: LineArt objects, and the ObstacleArt object that contains LineArt objects.

    Given that, it seems to me that a more appropriate approach would be to simply compose the MainWindowResource.Lines and MainWindowResource.Obstacles collections, and then use data templates to appropriately display these collections together.

    Here is a new version of your XAML that shows what I mean:

    
    
      
        
        
        
          
          
        
        
          
            
              
                
                
              
            
            
              
                
                
              
            
          
        
      
      
        
          
            
              
              
            
          
          
            
              
              
            
          
        
      
    
    

    The key here is the second DataTemplate, with a target type of ObstacleArt. This allows the main ItemsControl to display the individual ObstacleArt elements in the composite collection. Via the second data template, it does so by nesting an entirely new ItemsControl within for each ObstacleArt object, where that ItemsControl handles all of the rendering for the ObstacleArt object. Note that since the actual items in that nested ItemsControl object are themselves LineArt items, this winds up referring back to the DataTemplate for the LineArt type.

    The above works without any changes at all to your code-behind. That said, it is my opinion that you would be better off making your classes inherit DependencyObject, and then make the bindable properties dependency properties. WPF does support INotifyPropertyChanged of course, but you have a lot of explicit property notification code in there that would just go away if you were using the dependency-property paradigm.

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