2011-02-17 64 views
0

我有一個combox和一些文本框。現在組合框被綁定到一些集合。 文本框中的值取決於Combobox的選定值。 假設如果我有列表或對象B的數組說arrayB。然後文本框綁定到對象B的不同屬性。 所以說,如果comobox的選擇值爲1,那麼文本框應綁定到arrayB [1]。DataBinding Combobox值變化

回答

0

不知道這是什麼,你intended.But看看下面的代碼,讓我知道如果這有助於

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> 
相關問題