简体   繁体   中英

How to update a ComboBox when is bind to ObservableCollection?

I'm new in WPF and I'm trying to bind a ComboBox with an ObservableCollection. Bing is showing correct values,and when I try to add new values from same page combobox is updated. When I'm trying to add to collection one new item from another PageTest combobox doesn't show new values, only old ones. What I am doing wrong? Here is my first ComboTest window with ComboBox.

   <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <ComboBox ItemsSource="{Binding ObservableCollection}" Width="200" Height="50">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Price}"></TextBlock>
                        <TextBlock Text="{Binding BuildingType.A, UpdateSourceTrigger=PropertyChanged}"></TextBlock>
                        <TextBlock Text="{Binding BuildingType.B, UpdateSourceTrigger=PropertyChanged}"></TextBlock>
                        <TextBlock Text="{Binding BuildingType.C, UpdateSourceTrigger=PropertyChanged}"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
        <Button Grid.Column="1" Click="ButtonBase_OnClick" Width="100" Height="100"> To next page</Button>
    </Grid>

Code behind for this window:

   public partial class ComboTest : Page
    { 
        ComboViewModel temp = new ComboViewModel();
        public ComboTest()
        {
            InitializeComponent();
            DataContext = temp;
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {          
           NavigationService.Navigate(new PageTest(temp));
        }
    }

Here is my view model:

    public class ComboViewModel
    {
        public ObservableCollection<Building> ObservableCollection { get; set; } = new ObservableCollection<Building>()
        {
            new Building()
            {
                Price = "250",
                BuildingType = new BuildingType() {A = "BuildingTypeA", B = "BuildingTypeA", C = "BuildingTypeA"}
            },
            new Building()
            {
                Price = "250",
                BuildingType = new BuildingType() {A = "BuildingTypeB", B = "BuildingTypeB", C = "BuildingTypeB"}
            },
            new Building()
            {
                Price = "250",
                BuildingType = new BuildingType() {A = "BuildingTypeC", B = "BuildingTypeC", C = "BuildingTypeC"}
            }
        };

        public  void AddItem()
        {
            ObservableCollection.Add(new Building()
            {
                Price = "250",
                BuildingType = new BuildingType() { A = "BuildingTypeC", B = "BuildingTypeC", C = "BuildingTypeC" }
            });
        }
    }

And another page:

  <Grid>
        <Button Width="200" Height="100" Click="ButtonBase_OnClick">Add and Navigate Back</Button>
   </Grid>

And code for this page

 public partial class PageTest : Page
    {
        private ComboViewModel temp;
        //public PageTest()
        //{
        //    InitializeComponent();
        //}

        public PageTest(ComboViewModel comboViewModel)
        {
            temp = comboViewModel;
            InitializeComponent();
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            temp.AddItem();
            NavigationService.GoBack();
        }
    }

When you navigate back a new instance of combotest is created, hence changes made to collection are lost.

one way of achieving the same is setting the keepalive property of the root page (combotest).

KeepAlive="True"

check this navigation-overview#PageLifetime

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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