I am new to wpf and fighting with this issue for several days. I have a combobox which has dataview as itemssource. It displays the values correctly but selecteditem is always null when i re-run the application.
ChargeCodeValidValues objects that combobox is bound to:
public class ChargeCodeValidValues
{
#region Properties
public DataTable ChargeCodesValidValuesTable { get; set; }
public DataView dvChargeCodeValidValues { get; set; }
#endregion
#region Constructor
public ChargeCodeValidValues()
{
LoadChargeCodesValidValues();
}
public void LoadChargeCodesValidValues()
{
Database db = new Database();
DataTable dataTable = db.ExecuteQuery("upGet_ChargeCodesValidValues", "ChargeCodesValidValues", "ID");
this.ChargeCodesValidValuesTable = dataTable;
this.dvChargeCodeValidValues = ChargeCodesValidValuesTable.DefaultView;
}
XAML:
<DataTemplate x:Key="combodescriptionTemplate">
<ComboBox Name="cboCombo"
Loaded="cboCombo_Loaded">
<ComboBox.DataContext>
<Objects:ChargeCodeValidValues/>
</ComboBox.DataContext>
</ComboBox>
</DataTemplate>
<local:TotalCellTemplateSelector x:Key="totalcellTemplateSelector"
combodescriptionTemplate="{StaticResource combodescriptionTemplate}"/>
gridview column :
<dxg:GridColumn Header="Description" FieldName="Description" Width="Auto" MinWidth="132" AllowMoving="False" CellTemplateSelector="{StaticResource totalcellTemplateSelector}" /> -->
this column is textbox by default. It changes to combobox based on values from other column
code:
private void cboCombo_Loaded(object sender, RoutedEventArgs e)
{
ComboBox cmb = sender as ComboBox;
DataTable dtChargeCodeValidValues = oChargeCodesValidvalues.ChargeCodesValidValuesTable.Copy();
DataView dvCurrentCodeValues = dtChargeCodeValidValues.Copy().DefaultView;
cmb.ItemsSource = dvCurrentCodeValues;
cmb.DisplayMemberPath = "Description";
cmb.SelectedValuePath = "Description";
cmb.SelectedValue = "Description";
}
ObservableCollection and DataBinding
that way wpflog.blogspot.com/2009/04/… – MethodMan