I have a Datagrid and inside first column I have a ComboxBox which holds list of Fee Types.User will need to select the type of fee and enter amount in the next column which is a textbox.I have binded this with the ObservableCollection but the list is not showing up.This is my xaml,see I have binded a observablecollection 'lstFeeType' which is a list of FeeType e.g Admission,Practicle etc.
<DataGrid Name="FTDataGrid" AutoGenerateColumns="False" CanUserAddRows="False" Grid.Row="5" Grid.ColumnSpan="2" ScrollViewer.VerticalScrollBarVisibility="Visible" Height="120" >
<DataGrid.Columns>
<DataGridTemplateColumn Header="Fee Type" Width="*" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=FeeTypeCollection}" Grid.Row="2" Grid.Column="1" Name="cmbFeeType" Width="165" Height="25" SelectedValuePath="Id" DisplayMemberPath="Description" SelectedItem="{Binding FeeType}" SelectedValue="{Binding FeeType}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Amount" Width="*" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Name="tbFeeAmount" Height="25" Width="140" Text="{Binding Path=Amount}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="+" Width="Auto" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button HorizontalAlignment="Center" VerticalAlignment="Center" Background="#2BB3EE" Foreground="#FFFDFAFA" Width="20" Height="20" Style="{StaticResource GelButton}" Click="AddRow_Click" >+</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="-" Width="Auto" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button HorizontalAlignment="Center" VerticalAlignment="Center" Background="#2BB3EE" Foreground="#FFFDFAFA" Width="20" Height="20" Style="{StaticResource GelButton}">-</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Code:
ObservableCollection<FeeCollection> _lstFee;
public AddEditFee()
{
_lstFee = new ObservableCollection<FeeCollection>() { new FeeCollection() { FeeTypeCollection = FeeTypeCollection() } };
InitializeComponent();
FTDataGrid.ItemsSource = _lstFee;
}
private ObservableCollection<Dictionary> FeeTypeCollection()
{
ObservableCollection<Dictionary> lstFeeType = new ObservableCollection<Dictionary>() { new Dictionary() { Id = 1, Description = "Admission" },new Dictionary() { Id = 1, Description = "Tution" },new Dictionary() { Id = 1, Description = "Annual" }, new Dictionary() { Id = 1, Description = "Practicle" }};
return lstFeeType;
}
private void Cancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void AddRow_Click(object sender, RoutedEventArgs e)
{
_lstFee.Add(new FeeCollection() { FeeTypeCollection = FeeTypeCollection() });
}
private void Save_Click(object sender, RoutedEventArgs e)
{
int a =FTDataGrid.Items.Count-1;
FeeCollection docPresObj = new FeeCollection();
docPresObj = (FeeCollection)(FTDataGrid.Items[a]);
}
Classes:
public class Dictionary
{
public int Id { get; set; }
public string Description { get; set; }
}
public class FeeCollection
{
public Dictionary FeeType { get; set; }
public double Amount { get; set; }
public ObservableCollection<Dictionary> FeeTypeCollection { get; set; }
}
What am I doing wrong,Please guide me.
My guess would be that since FTDataGrids ItemSource is set to _lstFee
it can't find lstFeeType
in FeeCollection (should normally be shown as a binding error). I would try placing "lstFeeType" as a property into the FeeCollection
class .