简体   繁体   中英

Silverlight add Binding To Controls

public class Person

    {

    private int _Id;
    public int Id{get{return value;} set{_Id=value;}}

    private string _Code
    public  string Code{get{return _Code;} set {_Code=value;}}

    private string _Name;
    public string Name{get{return _Name;}set{_Name=value;}}

    } 

this is my model class

I have a two Combobox in my form.Combo1 and Combo2. Combo1 DisplayMember Code ValueMember Id
Combo2 DisplayMember Name ValueMember Id

I want to that when I change the Code or Name another combobox edit value and display value change

I'd recommend several changes:

First, you might want to break your person class out into two classes: Code and Person

public class Person
{
    public int Id { get; set; }
    public int CodeId { get; set; }
    public string Name { get; set; }
}

public class Code
{
    public int Id { get; set; }
    public string CodeText { get; set; }
}

Then create a class that you'll set to your view's datacontext, normally called a viewmodel in the MVVM design pattern:

public class Data : INotifyPropertyChanged
{
    public List<Person> People { get; set; }
    public List<Code> Codes { get; set; }

    private Code _selectedCode;
    public Code SelectedCode
    {
        get
        {
            return _selectedCode;
        }
        set
        {
            _selectedCode = value;
            PropertyChanged(this, new PropertyChangedEventArgs("SelectedCode"));
            SelectedPeople = People.Where(p => p.CodeId == SelectedCode.Id).ToList();
        }
    }

    private List<Person> _selectedPeople;
    public List<Person> SelectedPeople
    {
        get
        {
            return _selectedPeople;
        }
        set
        {
            _selectedPeople = value;
            PropertyChanged(this, new PropertyChangedEventArgs("SelectedPeople"));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

Notice that it implements INotifyPropertyChanged. This is crucial with binding in Silverlight. In the constructor of your MainPage.xaml.cs:

Data data = new Data
    {
        Codes = new List<Code>(),// populate your codes

        People = new List<Person>() // populate your people, giving them correct codeids
    }

this.DataContext = data;

Now in your view's XAMl:

<ComboBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" ItemsSource="{Binding Codes}" SelectedItem="{Binding SelectedCode, Mode=TwoWay}" DisplayMemberPath="CodeText" />
<ComboBox Height="23" HorizontalAlignment="Left" Margin="12,54,0,0" Name="comboBox2" VerticalAlignment="Top" Width="120" ItemsSource="{Binding SelectedPeople}" DisplayMemberPath="Name" />

Notice the first combobox binds to the Codes collection on the DataContext. The selectedItem property TWO-WAY binds to the SelectedCode property on the datacontext. When the user changes that selected item, the setter on the datacontext gets called. We updated the list of peopel to show, and raise the PropertyChanged event which notifies the view that it needs to update the people box.

Of course, this would be much cleaner if you properly implemented the MVVM design pattern. I like to use the MVVM Light toolkit for this.

I know that was long-winded, but I wanted to guide you down a correct path so you could use this knowledge on this project and in the future:)

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