0
votes

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";
 }
1
are you familiar with ObservableCollection and DataBinding that way wpflog.blogspot.com/2009/04/…MethodMan
Not that much familiar. But i do not think that example can be applied on my situation.Saru

1 Answers

0
votes

There is no magic that will persist a selection in a ComboBox across different invocations of your application; you'll have to write the code yourself. For example:

<ComboBox
    x:Name="comboBox"
    SelectionChanged="OnSelectionChanged"
    Loaded="OnComboBoxLoaded"
    >
    <ComboBox.Items>
        <system:String>One</system:String>
        <system:String>Two</system:String>
        <system:String>Three</system:String>
    </ComboBox.Items>
</ComboBox>

...with the following code-behind:

private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    int index = comboBox.SelectedIndex;
    using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (IsolatedStorageFileStream stream = store.CreateFile("selectedIndex"))
        {
            using (BinaryWriter writer = new BinaryWriter(stream))
            {
                writer.Write(index.ToString());
            }
        }
    }
}

private void OnComboBoxLoaded(object sender, RoutedEventArgs e)
{
    int index = GetPreviousIndex();
    comboBox.SelectedIndex = index;
    comboBox.Text = comboBox.Items[index] as string;
}

private int GetPreviousIndex()
{
    using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (IsolatedStorageFileStream stream = store.OpenFile("selectedIndex", FileMode.Open))
        {
            using (BinaryReader reader = new BinaryReader(stream))
            {
                try
                {
                    return int.Parse(reader.ReadString());
                }
                catch (Exception x)
                {
                    return -1;
                }
            }
        }
    }
}

Note that setting the SelectedIndex is insufficient; you have to set the Text property as well, or you won't see the selection without opening the ComboBox.

There are many ways of persisting the selection, of course; here I just cobbled something together quickly (and somewhat dirtily).