I have an Organization entity and a Region entity. Organization type The object can have one or more Region objects connected to it, so my Region entity has a foreign key to the organization entity. Use WCF RIA and Entity Framework to extract the Organization and Region objects from my database. I want to change the Organization The object is placed in a ComboBox, the Region object is placed in another ComboBox, and when the organization of the ComboBox with the Region object is selected, only the area connected to the selected organization is automatically displayed. It should be very basic, but I design it now The way does not work at all.
So, any hint how I can achieve this goal? A simple simple code example is very much appreciated!
(I am using SL4, WCF RIA MVVM)
EDIT2 EDIT2 EDIT2 EDIT2 EDIT2 EDIT2 EDIT2 EDIT2 EDIT2 EDIT2 EDIT2 EDIT2:
Using Venomo’s ElemntBinding answer, when I want to This is very useful for me when adding a new object to my collection, I just pull the available countries and connection areas and type a website in the text box… so I get my organization, area and Combination of sites:)
Now, when I want to edit a website in my collection, I encountered a new problem. In the edit mode, I want to pre-select and disable the two drop-down menus ( BusinessRule is that I can edit the website name, not the organization area it is connected to). Therefore, by setting the SelectedIndex property on the Organization combobox, I selected my organization, but when the same operation is performed in the Regions combobox, it will Failed due to Object Reference error.
Basic example:
Suppose we have a simple class like this:
public class Country
{
public string Name {get; set; }
public IEnumerableRegions {get; set; }
}
Then we will There are two combo boxes: one for selecting a country and the other for selecting the country/region. When the value of the first one changes, the second one should update itself.
Okay, First we must tell Silverlight how to display countries/regions. For complex scenes, we can use DataTemplate, but for now, we only need the DisplayMemberPath property of the ComboBox class.
Therefore , We create a simple collection of these objects in the following code:
var countries = new List()
{
new Country< br /> {
Name = "USA",
Regions = new List
{
"Texas", "New York", "Florida", ...
},
},
new Country
{
Name = "UK",
Regions = new List
{< br /> "Scotland", "Wales", "England", ...
},
},
...
};
I know these are not all regions of the sample country, but this is a Silverlight example, not a geography course.
Now, we have to set the ItemsSource of the ComboBox to this collection.
< p>
cbCountries.ItemsSource = countries;
Both of these can be in the code-behind constructor.
Okay, now back to XAML!
We need another ComboBox and a way to tell it to dynamically get its items from other collections.
Binding its ItemsSource to the selected items of another ComboBox is to achieve this The most obvious way to target.
pre> This should be easy.
If you use MVVM:
You can bind from the ViewModel to the ItemsSource of the first ComboBox. The rest remains the same.< br>To tell the ViewModel what the selected value is, use two-way binding for the SelectedItem properties of the two ComboBoxes and bind it to any property provided for it in the ViewModel.
If your collection Can be changed dynamically:
If the country/region list (or whatever you want to use it) can be changed at runtime, it is best to implement INotifyPropertyChanged for Country classes and regions, use ObservableCollection<>. If you don’t need to change during runtime, you don’t need to worry about it.
Edit: The following problem is fixed, go to EDIT2 in this article.
< /p>
I have an Organization entity and a Region entity. The organization type object can have one or more Region objects connected to it, so my Region entity has a foreign key to the organization entity. Use WCF RIA And Entity Framework extracts the Organization and Region objects from my database. I want to put the Organization object in one ComboBox and the Region object in another ComboBox, and automatically show only the connection when selecting the organization of the ComboBox with the Region object To the selected organization area. It should be very basic, but the way I design it now doesn’t work at all.
So, any hint how I can achieve this goal? A simple simple code example is very much appreciated!
(I am using SL4, WCF RIA MVVM)
EDIT2 EDIT2 EDIT2 EDIT2 EDIT2 EDIT2 EDIT2 EDIT2 EDIT2 EDIT2 EDIT2 EDIT2:
Using Venomo’s ElemntBinding answer, when I want to This is very useful for me when adding a new object to my collection, I just pull the available countries and connection areas and type a website in the text box... so I get my organization, area and Combination of sites:)
Now, when I want to edit a website in my collection, I encountered a new problem. In the edit mode, I want to pre-select and disable the two drop-down menus ( BusinessRule is that I can edit the website name, not the organization area it is connected to). Therefore, by setting the SelectedIndex property on the Organization combobox, I selected my organization, but when the same operation is performed in the Regions combobox, it will Failed due to Object Reference error.
You can achieve this with some clever ElementBindings.
Basic example:
Suppose we have a simple class like this:
public class Country
{
public string Name {get; set ; }
public IEnumerableRegions {get; set; }
}
Then, we will have two combo boxes: one for selecting a country , The other is used to select the country/region. When the value of the first one changes, the second one should update itself.
Ok, first we must tell Silverlight how to display the country/region. For For complex scenarios, we can use DataTemplate, but for now, we only need the DisplayMemberPath property of the ComboBox class.
So, we create a simple collection of these objects in the following code:
var countries = new Lis t()
{
new Country
{
Name = "USA",
Regions = new List
{
"Texas", "New York", "Florida", ...
},
},
new Country
{
Name = "UK" ,
Regions = new List
{
"Scotland", "Wales", "England", ...
},
},
...
};
I know these are not all regions in the sample country, but this is a Silverlight example, not a geography course.
Now, we must set the ItemsSource of the ComboBox to this collection.
cbCountries.ItemsSource = countries;
Both of these can be constructed in the code-behind In the function.
Ok, now back to XAML!
We need another ComboBox and a way to tell it to dynamically get its items from other collections.
Binding its ItemsSource to the selected items of another ComboBox is to achieve this The most obvious way to target.
pre> This should be easy.
If you use MVVM:
You can bind from the ViewModel to the ItemsSource of the first ComboBox. The rest remains the same.< br>To tell the ViewModel what the selected value is, use two-way binding for the SelectedItem properties of the two ComboBoxes and bind them to any properties provided for them in the ViewModel.
If your collection Can be changed dynamically:
If the country/region list (or whatever you want to use it) can be changed at runtime, it is best to implement INotifyPropertyChanged for Country classes and regions, use ObservableCollection<>. If you don't need to change during operation, you don't need to worry about this.