當用戶在文本框中輸入內容時,我想強制下拉框打開。MVVM/WPF - 當用戶在文本框中輸入時總是顯示組合框下拉列表
我有邏輯來過濾列表,當我不綁定任何東西到IsDropDownOpen元素,它似乎工作如所需,除非如果文本框失去焦點,然後重新調整,下拉菜單不會回來,除非我點擊箭頭。
<ComboBox x:Name="nodeList"
Text="{Binding UserInput, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding NodeTokenList}" DisplayMemberPath="FullNodeName"
HorizontalAlignment="Left" VerticalAlignment="Top" Width="324"
Margin="162,49,0,0" Height="32"
IsDropDownOpen="{Binding Path=ShowDropDown, Mode=TwoWay, Converter= {StaticResource BoolToVis}, UpdateSourceTrigger=PropertyChanged}" />
以上是我的xaml。
每次UserInput被更改(文本的項目源)時,它會過濾下拉列表的項目源。我現在有它,這樣ShowDropDown(我BOOL爲IsDropDownOpen)
這是我的UserInput和ShowDropDown定義:
public string UserInput
{
get { return _userInput; }
set
{
_userInput = value;
FilterListOnKeyPress();
OnPropertyChanged("UserInput");
}
}
public bool ShowDropDown
{
get { return _showDropDown; }
set
{
_showDropDown = value;
OnPropertyChanged("ShowDropDown");
}
}
,並終於在這裏是我的執行FilterListOnKeyPress()的
private void FilterListOnKeyPress()
{
if (string.IsNullOrEmpty(UserInput))
{
NodeTokenList = BackUpNodeTokenList;
return;
}
else
{
NodeTokenList = BackUpNodeTokenList.Where(x => x.FullNodeName.ToLower().Contains(UserInput.ToLower())).ToList();
ShowDropDown = true;
}
}
我不知道在這種情況下AutoCompleteBox可能是更好的選擇。 – ChrisF
您可能需要明確處理GetFocus,例如:http://stackoverflow.com/questions/1217268/how-to-get-notified-when-a-window-get-focus-in-wpf –
'ShowDropDown '不是mvvm。而MVVM並不意味着「沒有隱藏代碼」。向用戶顯示下拉菜單是UI功能。處理UI中的邏輯。 – Will