如何在mvvm中将自动完成框与模型绑定?

时间:2013-02-15 10:31:22

标签: mvvm wpftoolkit

我公开了一个集合并将其绑定到自动完成框的项目源,但它可以正常工作,但选择或更改自动完成框上的文本不会像文本框或标签那样更新模型!

视图模型:

public ObservableCollection<String> SymptomsDb { get; private set; }

private String symptom;

public String Symptom
{
    get { return symptom; }
    set
    {
        symptom = value;
        RaisePropertyChanged(() => this.Symptom);
    }
}

public AnalysisViewModel()
{
    List<String> s = new List<String>();
    s.Add("test");
    SymptomsDb = new ObservableCollection<String>(s);
}

视图:

<controls:AutoCompleteBox 
    ItemsSource="{Binding SymptomsDb}" 
    SelectedItem="{Binding Symptom}" 
    Text="{Binding Symptom}" 
    IsTextCompletionEnabled="True" 
    FilterMode="Contains"/>

1 个答案:

答案 0 :(得分:8)

要从用户界面更改回viewmodel,您将始终需要绑定属性TwoWay(除了default之外的某些属性,如TextBox.TextProperty,它们是TwoWay):

<controls:AutoCompleteBox 
    ItemsSource="{Binding SymptomsDb}" 
    SelectedItem="{Binding Symptom, Mode=TwoWay}" 
    Text="{Binding Symptom}" 
    IsTextCompletionEnabled="True" 
    FilterMode="Contains"/>