Reputation: 85
I have a combo box inside the datagrid. Datagrid is bound to an item source. I want to fill the combo box to a collection apart from its parent collection but its not working.
<data:DataGrid x:Name="dgTransferStockroomGLDetails" AutoGenerateColumns="False" ColumnHeaderStyle="{StaticResource DataGridColumnHeaderStyle}"
VerticalScrollBarVisibility="Visible" ItemsSource="{Binding StockroomTransferDetails, Mode=TwoWay}"
CanUserResizeColumns="False" VerticalAlignment="Top" RowBackground="White" AlternatingRowBackground="White" GridLinesVisibility="All" Height="400">
<data:DataGrid.Columns>
<data:DataGridTemplateColumn Header="From Stockroom" Width="200" CanUserReorder="True" CanUserSort="True" IsReadOnly="False">
<data:DataGridTemplateColumn.HeaderStyle>
<Style TargetType="prim:DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="FontWeight" Value="Bold"/>
</Style>
</data:DataGridTemplateColumn.HeaderStyle>
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<!--<TextBox Text="{Binding From_Stkrm_Id}" Width="200" Height="30" />-->
<ComboBox Width="200" Height="30" ItemsSource="{Binding WiingsStkrmList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Name" />
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
</data:DataGrid.Columns>
</data:DataGrid>
Here is my view model
private ObservableCollection<BuyerWebService.Stockroom> wiingsStkrmList;
public ObservableCollection<BuyerWebService.Stockroom> WiingsStkrmList
{
get
{
return wiingsStkrmList;
}
set
{
wiingsStkrmList = value;
SendChangedNotification("WiingsStkrmList");
}
}
private ObservableCollection<BuyerWebService.StockroomTransfer> stockroomTransferdetails;
public ObservableCollection<BuyerWebService.StockroomTransfer> StockroomTransferDetails
{
get
{
return stockroomTransferdetails;
}
set
{
stockroomTransferdetails = value;
SendChangedNotification("StockroomTransferDetails");
}
}
I checked the output window, it says 'WiingsStkrmList' property not found on 'BuyerPortal.BuyerWebService.StockroomTransfer'
StockroomTransfer is used to bind the datagrid. The combo is trying to find the WiingsStkrmList in parent item source.
How to bind it to a different collection?
Upvotes: 0
Views: 106
Reputation: 6415
You need to specify a relative source for where the bound property can be found, usually by pointing at a parent element that is bound to the view model. Something like this:
ItemsSource="{Binding StockroomTransferDetails, RelativeSource={RelativeSource AncestorType=Grid}}"
Change AncestorType=Grid to something that has a datacontext of your viewmodel ... something outside of your datagrid basically.
Upvotes: 1