简体   繁体   English

将对象的值绑定到存储在列表中的组合框

[英]Binding a value of an object to a combobox which is stored in a list

I want to bind a value of each object stored in a ObersvableCollection to the ComboBox. 我想将存储在ObersvableCollection中的每个对象的值绑定到ComboBox。 Unfortunately it doesn't work. 不幸的是,它不起作用。

<ComboBox Grid.Column="1" Margin="0,0,0,5" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize="15" ItemsSource="{Binding Path=UserAccounts}" >
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding EMailAddress}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

In the code behind I set the DataContext: 在后面的代码中,我设置了DataContext:

this.DataContext = PersitentDataModelUserSettings.Instace.UserAccounts;

Here is the Model: 这是模型:

public class PersitentDataModelUserSettings : ModelBase
{
    private static PersitentDataModelUserSettings instance = new PersitentDataModelUserSettings();

    public static PersitentDataModelUserSettings Instance
    {
        get { return instance; }
        set { instance = value; } 
    }

    private ObservableCollection<AccountSettingsObj> _userAccounts = new ObservableCollection<AccountSettingsObj>();

    public ObservableCollection<AccountSettingsObj> UserAccounts
    {
        get { return _userAccounts; }
        set
        {
            _userAccounts = value;
            OnPropertyChanged("UserAccounts");
        }
    }
}

This is the object type: 这是对象类型:

[XmlRoot]
public class AccountSettingsObj : ModelBase
{
    public string FirstName { get; set; }

    public string LastName { get; set; }

    private string _eMailAddress; 
    public string EMailAddress 
    {
        get { return _eMailAddress; }
        set 
        {
            _eMailAddress = value;
            OnPropertyChanged("EMailAddress");
        }
    }
}

Thanks! 谢谢!

You are setting ItemSource of comboBox to this: 您正在将comboBox的ItemSource设置为此:

ItemsSource="{Binding Path=UserAccounts}"

but you also setting DataContext of ComboBox to this: 但您也可以将ComboBox的DataContext设置为此:

this.DataContext = PersitentDataModelUserSettings.Instace.UserAccounts;

So binding engine is looking for property UserAccounts in UserAccounts . 因此,绑定引擎正在UserAccounts中寻找属性UserAccounts


This can be fixed in two ways: 这可以通过两种方式解决:

  • Either set ItemsSource to binding like this: ItemsSource="{Binding}" . 可以将ItemsSource设置为这样的绑定: ItemsSource="{Binding}" This way you are saying ItemSource is same as DataContext of ComboBox. 这样,您说的是ItemSource与ComboBox的DataContext相同。

  • Or set DataContext to instance only : this.DataContext = PersitentDataModelUserSettings.Instace.UserAccounts; 或将DataContext设置为仅实例: this.DataContext = PersitentDataModelUserSettings.Instace.UserAccounts; . This way you set DataContext to point to instance of PersitentDataModelUserSettings and asking BindingEngine to look for property UserAccounts in PersitentDataModelUserSettings . 这样,您可以将DataContext设置为指向PersitentDataModelUserSettings的实例,并让BindingEngine在PersitentDataModelUserSettings中查找属性UserAccounts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM