DataBinding Combobox value change
I have a combox and some textboxes. Now the combobox is binded to some collection. the value in the textboxes depend on the selected value of Combobox. Suppose if I have List or array of object B say arrayB. Then the textboxes are binded to different property of object B. So say if co开发者_开发技巧mobox has the selected value of 1 then the textboxes should be bound to arrayB[1].
Not sure this is what you intended.But have a look at the below code and let me know if this helps
public class Animal
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private Category category;
public Category Category
{
get { return category; }
set { category = value; }
}
public Animal(string name, Category category)
{
this.name = name;
this.category = category;
}
}
public enum Category
{
Amphibians,
Bears,
BigCats,
Canines,
Primates,
Spiders,
}
public class Animals
{
private List<Animal> animalList;
public IEnumerable<Animal> AnimalList
{
get { return animalList; }
}
public Animals()
{
animalList = new List<Animal>();
animalList.Add(new Animal("California Newt", Category.Amphibians));
animalList.Add(new Animal("Giant Panda", Category.Bears));
animalList.Add(new Animal("Coyote", Category.Canines));
animalList.Add(new Animal("Golden Silk Spider", Category.Spiders));
animalList.Add(new Animal("Mandrill", Category.Primates));
animalList.Add(new Animal("Black Bear", Category.Bears));
animalList.Add(new Animal("Jaguar", Category.BigCats));
animalList.Add(new Animal("Bornean Gibbon", Category.Primates));
animalList.Add(new Animal("African Wildcat", Category.BigCats));
animalList.Add(new Animal("Arctic Fox", Category.Canines));
animalList.Add(new Animal("Tomato Frog", Category.Amphibians));
animalList.Add(new Animal("Grizzly Bear", Category.Bears));
animalList.Add(new Animal("Dingo", Category.Canines));
animalList.Add(new Animal("Gorilla", Category.Primates));
animalList.Add(new Animal("Green Tree Frog", Category.Amphibians));
animalList.Add(new Animal("Bald Uakari", Category.Primates));
animalList.Add(new Animal("Polar Bear", Category.Bears));
animalList.Add(new Animal("Black Widow Spider", Category.Spiders));
animalList.Add(new Animal("Bat-Eared Fox", Category.Canines));
animalList.Add(new Animal("Cheetah", Category.BigCats));
animalList.Add(new Animal("Cheetah", Category.Spiders));
}
}
Xaml
<Window.Resources>
<local:Animals x:Key="animals"/>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ComboBox Name="cmb" VerticalContentAlignment="Center" Width="100" Height="30" ItemsSource="{Binding Path=AnimalList, Source={StaticResource animals}}" DisplayMemberPath="Name"/>
<TextBox Width="100" Height="30" Grid.Column="1" Text="{Binding ElementName=cmb,Path=SelectedItem.Category}"/>
</Grid>
精彩评论