Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF ComboBox Binding To Object On Initial Load

I have a combo box that is bound to a list of model objects. I've bound the combo box SelectedItem to a property that is the model type. All of my data binding works beautifully after the window has been loaded. The SelectedItem is set properly and I'm able to save the object directly with the repository.

The problem is when the window first loads I initialize the SelectedItem property and my combobox displays nothing. Before I moved to binding to objects I was binding to a list of strings and that worked just fine on initialization. I know I'm missing something but I can't figure it out.

Thanks in advance for any guidance you can provide.

(One note about the layout of this page. The combo boxes are actually part of another ItemTemplate that is used in a ListView. The ListView is bound to an observable collection in the main MV. Each item of this observable collection is itself a ModelView. It is that second ModelView that has the SelectedItem property.)

Here is my Model:

    public class DistributionListModel : Notifier, IComparable
    {
    private string m_code;
    private string m_description;

    public string Code
    {
        get { return m_code; }
        set { m_code = value; OnPropertyChanged("Code"); }
    }

    public string Name
    {
        get { return m_description; }
        set { m_description = value; OnPropertyChanged("Name"); }
    }

    #region IComparable Members

    public int CompareTo(object obj)
    {
        DistributionListModel compareObj = obj as DistributionListModel;
        if (compareObj == null)
            return 1;

        return Code.CompareTo(compareObj.Code);
    }

    #endregion
}

Here the pertinent code in my ModelView:

public MailRoutingConfigurationViewModel(int agencyID)
    : base()
{
    m_agencyID = agencyID;
    m_agencyName = DataManager.QueryEngine.GetAgencyName(agencyID);

    IntializeValuesFromConfiguration(DataManager.MailQueryEngine.GetMailRoutingConfiguration(agencyID));

    // reset modified flag
    m_modified = false;
}

private void IntializeValuesFromConfiguration(RecordCheckMailRoutingConfiguration configuration)
{
    SelectedDistributionList = ConfigurationRepository.Instance.GetDistributionListByCode(configuration.DistributionCode);
}

public DistributionListModel SelectedDistributionList
{
    get { return m_selectedDistributionList; }
    set
    {
        m_selectedDistributionList = value;
        m_modified = true;
        OnPropertyChanged("SelectedDistributionList");
    }

}

And finally the pertinent XAML:

<UserControl.Resources>
        <DataTemplate x:Key="DistributionListTemplate">
            <Label Content="{Binding Path=Name}" />
        </DataTemplate>
</UserControl.Resources>
              <ComboBox 
                       ItemsSource="{Binding Source={StaticResource DistributionCodeViewSource}, Mode=OneWay}"
                       ItemTemplate="{StaticResource DistributionListTemplate}"
                                        SelectedItem="{Binding Path=SelectedDistributionList, Mode=TwoWay}"
                        IsSynchronizedWithCurrentItem="False"
                                         />
like image 493
SRM Avatar asked Oct 16 '25 07:10

SRM


1 Answers

@SRM, if I understand correctly your problem is binding your comboBox to a collection of objects rather than a collection of values types ( like string or int- although string is not value type). I would suggest add a two more properties on your combobox

<ComboBox 
  ItemsSource="{Binding Source={StaticResource DistributionCodeViewSource}, 
                        Mode=OneWay}"                        
  ItemTemplate="{StaticResource DistributionListTemplate}"
  SelectedItem="{Binding Path=SelectedDistributionList, Mode=TwoWay}"
  SelectedValuePath="Code"  
  SelectedValue="{Binding SelectedDistributionList.Code }"/>

I am assuming here that DistributionListModel objects are identified by their Code. The two properties I added SelectedValuePath and SelectedValue help the combobox identify what properties to use to mark select the ComboBoxItem by the popup control inside the combobox. SelectedValuePath is used by the ItemSource and SelectedValue by for the TextBox.

like image 60
Arjun Avatar answered Oct 17 '25 20:10

Arjun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!