MVVM and Databinding with UniformGrid

后端 未结 1 1905
一生所求
一生所求 2021-02-10 14:43

I\'m trying to style the back of a WPF chart with some rectangles. I\'m using MVVM, and I need the rectangles to be uniformly sized. When defined via Xaml, this works with a fix

1条回答
  •  一生所求
    2021-02-10 15:18

    You could use ItemsControl to Bind like this. Simple example where ItemsSource is just an ObservableCollection

    
        
            
                
                    
                        
                    
                
                
                    
                        
                    
                
            
        
    
    

    Update
    It works for my usage scenario, but I might be missing something here. Here's the full code I've tried. I get the same result from both

    MainWindow.xaml

    
        
            
                
                    
                        
                            
                                
                            
                        
                        
                            
                                
                            
                        
                    
                    
                
            
        
    
    

    MainWindow.xaml.cs

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            BrushConverter brushConverter = new BrushConverter();
            MyBrushes = new ObservableCollection();
            MyBrushes.Add(brushConverter.ConvertFrom("#22ADD8E6") as Brush);
            MyBrushes.Add(brushConverter.ConvertFrom("#22D3D3D3") as Brush);
            MyBrushes.Add(brushConverter.ConvertFrom("#22ADD8E6") as Brush);
            MyBrushes.Add(brushConverter.ConvertFrom("#22D3D3D3") as Brush);
            this.DataContext = this;
        }
    
        public ObservableCollection MyBrushes
        {
            get;
            set;
        }
    }
    

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