简体   繁体   English

WPF组合框绑定以更新多个文本框

[英]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 我有一个列表(ListNames),其中包含添加到其中的名称,并使用组合框进行绑定

<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. 我假设您正在使用MVVM。

You need to create in your window's viewmodel, a property "CurrentName" of type "Names", binded to the ComboBox SelectedItem property. 您需要在窗口的视图模型中创建类型为“名称”的属性“ CurrentName”,该属性绑定到ComboBox SelectedItem属性。 This property must raise the NotifyPropertyChanged event. 此属性必须引发NotifyPropertyChanged事件。

Then, bind your label field, to this "CurrentName" property. 然后,将您的标签字段绑定到此“ CurrentName”属性。 When the SelectedIem property will change on the combobox, your label field will then be updated. 当组合框上的SelectedIem属性更改时,您的标签字段将被更新。

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: 您的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM