Reputation: 85
I have a data grid which is bound to an item source. Inside data grid i have a combo box whose item source is in the view model. I have set the selected item for the combo box which is coming from the item source of the data grid but it doesn't get selected. Combo Box is having items but respective item is not getting selected for each row.
<navigation:Page.Resources>
<VM:TransferStockroomGLViewModel x:Key="TransferStockroomGLViewModel" />
</navigation:Page.Resources>
<data:DataGrid x:Name="dgTransferStockroomGLDetails" AutoGenerateColumns="False" ColumnHeaderStyle="{StaticResource DataGridColumnHeaderStyle}"
VerticalScrollBarVisibility="Visible" ItemsSource="{Binding StockroomTransferDetails}"
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 Source={StaticResource TransferStockroomGLViewModel},Path=WiingsStkrmList}"
SelectedValuePath="From_Stkrm_Name" DisplayMemberPath="Name" />
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
</data:DataGrid.Columns>
</data:DataGrid>
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");
}
}
public TransferStockroomGLViewModel()
{
app = (App)Application.Current;
GetWiingsStockroomList();
GetStockroomTransferGLDetails();
}
public void GetStockroomTransferGLDetails()
{
try
{
bsc.GetStockroomTransferGLDetailsCompleted += new EventHandler<BuyerWebService.GetStockroomTransferGLDetailsCompletedEventArgs>(bsc_GetStockroomTransferGLDetailsCompleted);
bsc.GetStockroomTransferGLDetailsAsync(app.LogonSiteID);
}
catch (Exception ex)
{
LogException.CatchException(ex);
}
}
void bsc_GetStockroomTransferGLDetailsCompleted(object sender, BuyerWebService.GetStockroomTransferGLDetailsCompletedEventArgs e)
{
try
{
if (e.Result != null)
{
StockroomTransferDetails = e.Result;
}
}
catch (Exception ex)
{
LogException.CatchException(ex);
}
finally
{
bsc.GetStockroomTransferGLDetailsCompleted -= new EventHandler<BuyerWebService.GetStockroomTransferGLDetailsCompletedEventArgs>(bsc_GetStockroomTransferGLDetailsCompleted);
}
}
public void GetWiingsStockroomList()
{
try
{
bsc.GetWiingsStockroomListCompleted += new EventHandler<BuyerWebService.GetWiingsStockroomListCompletedEventArgs>(bsc_GetWiingsStockroomListCompleted);
bsc.GetWiingsStockroomListAsync(app.LogonSiteID);
}
catch (Exception ex)
{
LogException.CatchException(ex);
}
}
void bsc_GetWiingsStockroomListCompleted(object sender, BuyerWebService.GetWiingsStockroomListCompletedEventArgs e)
{
try
{
if (e.Result != null)
{
WiingsStkrmList = e.Result;
}
}
catch (Exception ex)
{
LogException.CatchException(ex);
}
finally
{
bsc.GetWiingsStockroomListCompleted -= new EventHandler<BuyerWebService.GetWiingsStockroomListCompletedEventArgs>(bsc_GetWiingsStockroomListCompleted);
}
}
I expect for each record the combobox should have the selected item but it is not coming.
What am i doing wrong?
Upvotes: 0
Views: 307
Reputation: 6172
It looks like you expect SelectedValuePath
to determine the selected value, but that is not the case. The property SelectedValue
will do that. The SelectedValuePath
determines how to find a property on each combobox-item that represents the very item and that can be used to check whether this is identical to the SelectedValue
.
Solution:
I assume your type BuyerWebService.Stockroom
has a string property Name
.
That means your bindings would look like this:
<ComboBox
ItemsSource="{Binding Source={StaticResource TransferStockroomGLViewModel}, Path=WiingsStkrmList}"
SelectedValuePath="Name"
SelectedValue="{Binding From_Stkrm_Name, Mode=TwoWay}"
DisplayMemberPath="Name"
Width="200" Height="30"/>
Upvotes: 1