2
votes

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?

2

2 Answers

2
votes

Use the DataContext property to set the bound object(s):

<DataTemplate>
  <local:AlarmView DataContext="{Binding}"/>
</DataTemplate>

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 the areaList objects.

0
votes

I would pass the list into the constructor of the enitity vm, then you can access it from there, because you can store it in a private member.

maybe there is another solution which might suit better, but for that I don't have enough details about the problem.