组合框结合在跌落时断裂

Ral*_*alt 0 c# data-binding silverlight

我有一个 在我的表格上ComboBox绑定的DependencyProperty.我可以通过Button点击向这个属性添加项目,它会将它们添加到ComboBox.但是,一旦我放下ComboBox绑定断裂.我仍然可以向该属性添加项目,但我ComboBox永远不会更新以显示它们.

例如

  • 单击Button三次
  • ComboBox下来-这将有三个项目
  • 再点击Button另外三次
  • ComboBox下来-它仍然将只有三个项目

再次启动应用程序并:

  • 单击Button六次
  • ComboBox下来-这将有六个项目

XAML

<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>
Run Code Online (Sandbox Code Playgroud)

代码背后

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++);
}
Run Code Online (Sandbox Code Playgroud)

dan*_*iel 6

使用ObservableCollection而不是List

ObservableCollection会引发在集合中添加和删除项目的属性更改,但List不会.