I’ve set up a datagrid that is bound to an ObservableCollection. One column in this grid is populated by a user control that needs to get data from the ObservableCollection that the parent datagrid is bound to. Is it possible to bind the child user control to use data from the ObervableCollection? The XAML I’m using is:
<Window x:Class="Hotspots_Control.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Hotspots_Control"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:viewModel/>
</Window.DataContext>
<DataGrid ItemsSource="{Binding areaList}" Name="hotspotsGrid" AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Area" Binding="{Binding Path=area}" IsReadOnly="True"/>
<DataGridTemplateColumn Header="Alarms">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<local:AlarmView/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
The grid is bound to “areaList” inside the “viewModel” object. I need to get the AlarmView user control to have access to each object in the ObservableCollection. Is there a way to do this?
Use the
DataContextproperty to set the bound object(s):Since the DataTemplate is already bound to the
areaList, you can set the DataContext with direct binding. From here, the child view will have access to theareaListobjects.