开发者

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;
        OnPropertyCh开发者_开发知识库anged("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"
                                         />


@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.


don't call your IntializeValuesFromConfiguration from the constructor, but after the load of the view.

A way to achieve that is to create a command in your viewmodel that run this method, and then call the command in the loaded event. With MVVM light toolkit, you can use the EventToCommand behavior... don't know mvvm framework you are using but there would probably be something like this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜