WPF – MVVM and data binding using UniformGRID

I am trying to use some rectangles to design the back of the WPF chart. I am using MVVM, and I need to resize the rectangles uniformly. When defined by Xaml, this applies to the fixed “BucketCount” 4:










How to bind my ObservableCollection rectangle? There is no “ItemsSource” property on UniformGrid. Do I need to use ItemsControl? If so, what should I do?

Thanks in advance.

You can use the ItemsControl binding like this. ItemsSource Just a simple example of ObservableCollection






< br />









Update
It works for my usage scenario, but I might be missing something here. This is the complete code I tried. I got the same result from both

MainWindow.xaml< /p>
















< br />



< /pre>

MainWindow.xaml.cs

public partial class MainWindow: Window
{
public MainWindow()
{< br /> 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 ObservableColl ection MyBrushes
{
get;
set;
}
}

I tried to use Some rectangles are used to design the back of the WPF chart. I am using MVVM and I need to resize the rectangles uniformly. When defined by Xaml, this applies to the fixed "BucketCount" 4:




< Rectangle Grid.Row="0" Grid.Column="0" Fill="#22ADD8E6" />





How to bind my ObservableCollection rectangle? There is no "ItemsSource" property on UniformGrid. Do I need to use ItemsControl? If so, what should I do?

Thanks in advance.

You can use the ItemsControl binding like this. ItemsSource is just a simple example of ObservableCollection








< /ItemsControl.ItemsPanel>







Update
It works for my usage scenario, but I might be missing something here. This is the complete code I tried. I got 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;
}
}

Leave a Comment

Your email address will not be published.