简体   繁体   中英

WPF Combobox binding to update multiple textbox

i have a class

class Names {
public int id get; set;};
public string name {get ; set};
public string til {set{
if (this.name == "me"){
return "This is me";
}
}

i have a list (ListNames) which contains Names added to it and binding with a combo box

<ComboBox  SelectedValue="{Binding Path=id, Mode=TwoWay,
 UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding
 ListNames}" DisplayMemberPath="name" SelectedValuePath="id" />

every thing works

i want to display the "Tip" on another label field when user selects an item.

is it possible?

help!

I'm assuming you're using MVVM.

You need to create in your window's viewmodel, a property "CurrentName" of type "Names", binded to the ComboBox SelectedItem property. This property must raise the NotifyPropertyChanged event.

Then, bind your label field, to this "CurrentName" property. When the SelectedIem property will change on the combobox, your label field will then be updated.

Something like this: Your Model:

public class Names 
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Tip {
        get
        {
            return Name == "me" ? "this is me" : "";
        }
    }
}

Your ViewModel:

public class ViewModel : INotifyPropertyChanged
{
    private ObservableCollection<Names> _namesList;
    private Names _selectedName;

    public ViewModel()
    {
        NamesList = new ObservableCollection<Names>(new List<Names>
            {
                new Names() {Id = 1, Name = "John"},
                new Names() {Id = 2, Name = "Mary"}
            });
    }

    public ObservableCollection<Names> NamesList
    {
        get { return _namesList; }
        set
        {
            _namesList = value;
            OnPropertyChanged();
        }
    }

    public Names SelectedName
    {
        get { return _selectedName; }
        set
        {
            _selectedName = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

And finally your View:

<Window x:Class="Combo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Combo="clr-namespace:Combo"
    Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
    <Combo:ViewModel/>
</Window.DataContext>
<StackPanel>
    <ComboBox ItemsSource="{Binding Path=NamesList}" DisplayMemberPath="Name" 
              SelectedValue="{Binding Path=SelectedName}"/>
    <Label Content="{Binding Path=SelectedName.Name}"/>
</StackPanel>

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