Reputation: 2124
I have a ComboBox
which is bound to a DependencyProperty
on my form. I can add items to this property through a Button
click and it will add them to the ComboBox
. However, as soon as I drop down the ComboBox
the binding breaks. I can still add items to the property, but my ComboBox
never updates to show them.
For example
Button
three timesComboBox
down - It will have three itemsButton
another three timesComboBox
down - It will still only have three itemsStarting the application again and:
Button
six timesComboBox
down - It will have six items<Grid x:Name="LayoutRoot"
Background="White">
<ComboBox Name="combTest"
ItemsSource="{Binding Users}"
Width="50"
Height="50"
HorizontalAlignment="Left" />
<Button Click="ButtonBase_OnClick"
Width="50"
Height="50"
HorizontalAlignment="Right" />
</Grid>
public MainPage()
{
InitializeComponent();
this.DataContext = this;
}
public static readonly DependencyProperty UsersProperty = DependencyProperty.Register(
"Users", typeof (List<string>), typeof (MainPage), new PropertyMetadata(new List<string>()));
public List<string> Users
{
get { return (List<string>) GetValue(UsersProperty); }
set { SetValue(UsersProperty, value); }
}
private int i = 0;
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
Users.Add("User " + i++);
}
Upvotes: 0
Views: 39
Reputation: 1070
Use ObservableCollection instead of List
ObservableCollection would raise property change of adding and removing item in the collection, but List not.
Upvotes: 6