当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Getting SelectedItem Issue on ComboBox - MVVM ObservableCollection

问题描述:

Firstly, my apologies to any one who may feel like this is a very dumb question or a question that has already been answered. I'm very new to MVVM, XAML and having read everything on StackOverflow, I'm still failing to understand what I'm sure is a simple concept.

So I have a MVVM set up where an Observable Collection is put into a ComboBox. Here is the view.

public class AppSettings : INotifyPropertyChanged

{

public class Regions

{

public int code { get; set; }

public string region { get; set; }

}

private ObservableCollection<Regions> _ItemsRegions = new ObservableCollection<Regions>

{

new Regions() { code = 0, region = "Metro : All Areas" },

new Regions() { code = 25, region = "Metro : North of River"},

new Regions() { code = 26, region = "Metro : South of River"},

new Regions() { code = 27, region = "Metro : East/Hills"},

};

public ObservableCollection<Regions> Region

{

get { return this._ItemsRegions; }

}

private Regions _selectedRegion;

public Regions SelectedRegion

{

get { return _selectedRegion; }

set

{

if (value != _selectedRegion)

{

_selectedRegion = value;

RaisePropertyChanged("SelectedRegion");

}

}

}

And for reference, here is my ComboBox.

<ComboBox Name="Regions" ItemsSource="{Binding Region}" DisplayMemberPath="region" SelectedItem="{Binding SelectedRegion, Mode=TwoWay}"/>

Now the good news is that I have the SelectedItem working and I'm able to with the MVVM obtain the values I need to make changes etc. which is great! That concept was a little difficult to grasp at first but once I had it, I knew what was going on. So I can save the data in any way at this point.... The eventual plan is to put it in roaming data. This is a Universal App.

So the problem is the user is finished with this page and navigates away from it . When he/she comes back to it the ComboBox is loaded with all the data (as expected) but the SelectedItem that I just saved is not. As in, the "IsSelected = True" item is null so it just says "choose an item".

What do I need to do in order to on page load, get the SelectedItem from save and set it to the ComboBox. I've been stupid and tried setting the Region.ComboBox.SelectedItem to a "string", but of course that defeats and destroys the whole MVVM purpose!

So, what do I need to do? I'm happy to be just pointed at some documentation for me to read.

Update Edit

So following advise from a user on here I took my get; set INotifyPropertyChanged logic and put it in a blank app. Just to see that I'm not doing anything stupid.

Unfortunately the blank app also has the same problem where navigation away from the page and back to it has none of my previously selected values... To illustrate this, here is a quick screenshot.

To navigate back, I'm using the hardware back button.

I'm guessing that I'm just missing a piece of the puzzle to make this work. This whole MVVM XAML concept is new to me and while I'm learning a lot about it, I'm genuinely stuck on this!

Any takers? Tips? Ideas? Examples?

Update 2

Okay so following some more advise posted in the comments on this question, I went and added a debugger track on the Object ID for "this._selectedRegion".

The watch of the Object ID shows that the data is in fact set correctly when a user selects one of the items from the combobox. Great! But when the user navigates away from the page and comes back to it, it's set to null. Not Great!

With my bland/stupid face am I missing the point completely? Am I meant to be on "OnNavigatedTo" aka, fist load setting the SelectedItem for the combobox myself? As in the data is never saved on navigation away and coming back to it is setting is null is in fact expected in MVVM? Do I manipulate the "get" statement and specify the ObservableCollection Item it's meant to be? Crude way, but like

_selectedRegion = something

return _selectedRegion

This sounds awfully crude to me and not what it should be, but maybe I'm just not appreciating the MVVM set up properly?

Thoughts anyone?

网友答案:

For a ComboBox in WPF it is very important to set SelectedItem to one of the items in ItemsSource.

Make sure that SelectedItem has the correct value (or is set) when he/she comes back.


Edit

Your binding

SelectedItem="{Binding SelectedRegion, Mode=TwoWay}"

will call the getter of SelectedRegion when the control is built. It will call the setter when the value in the ComboBox changes.

分享给朋友:
您可能感兴趣的文章:
随机阅读: