在这里,我在 MVVM 框架中有一个替代方案。
我的 xml 文件:
<ComboBox Name="cmbContains" IsEditable="True" IsTextSearchEnabled="false" ItemsSource="{Binding pData}" DisplayMemberPath="wTitle" Text="{Binding SearchText ,Mode=TwoWay}" >
<ComboBox.Triggers>
<EventTrigger RoutedEvent="TextBoxBase.TextChanged">
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsDropDownOpen">
<DiscreteBooleanKeyFrame Value="True" KeyTime="0:0:0"/>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ComboBox.Triggers>
</ComboBox>
我的cs文件:
//ItemsSource - pData
//There is a string attribute - wTitle included in the fooClass (DisplayMemberPath)
private ObservableCollection<fooClass> __pData;
public ObservableCollection<fooClass> pData {
get { return __pData; }
set { Set(() => pData, ref __pData, value);
RaisePropertyChanged("pData");
}
}
private string _SearchText;
public string SearchText {
get { return this._SearchText; }
set {
this._SearchText = value;
RaisePropertyChanged("SearchText");
//Update your ItemsSource here with Linq
pData = new ObservableCollection<fooClass>{pData.ToList().Where(.....)};
}
}
您可以看到可编辑的组合框正在绑定到字符串 (SearchText) 一旦出现 TextChanged 事件,就会显示下拉菜单,并且双向绑定会更新值。在进入 set{} 时,cs 文件中的 ItemsSource 发生了变化;句法。
上面代码的要点