.axaml File
<AutoCompleteBox Name="customFilterAutoCompleteBox" Items="{Binding MyItems}" Width="200" Text="{Binding Mode=OneWayToSource}" FilterMode="Custom">
<AutoCompleteBox.Styles>
<Style Selector="ListBoxItem">
<Setter Property="Background" Value="Red"/>
<Setter Property="Template">
<ControlTemplate TargetType="ListBoxItem">
<Border>
<TextBlock Text="{Binding Name}"/>
</Border>
</ControlTemplate>
</Setter>
</Style>
</AutoCompleteBox.Styles>
</AutoCompleteBox>
.axaml.cs 设置 AutoCompleteBox ItemFilter:
this.Get<AutoCompleteBox>("customFilterAutoCompleteBox").ItemFilter = (string keyWord, object sender) =>
{
var user = sender as User;
if (user != null && user.Name.ToLower().Contains(keyWord.ToLower()))
return true;
return false;
};
ViewModel 和 Model:
public class MainWindowViewModel : ViewModelBase
{
public string Greeting => "Welcome to Avalonia!";
public ObservableCollection<User> MyItems => new ObservableCollection<User>()
{
new User(){MyProperty=true,Name="The First Name" },
new User(){MyProperty=false,Name="The Last Name" },
new User(){MyProperty=true,Name="333 First 222" },
new User(){MyProperty=false,Name="222 Last 222" },
};
public MainWindowViewModel() { }
}
public class User : ViewModelBase
{
private bool _myProperty;
public bool MyProperty
{
get => _myProperty;
set => this.RaiseAndSetIfChanged(ref _myProperty, value);
}
private string _name;
public string Name
{
get => _name;
set => this.RaiseAndSetIfChanged(ref _name, value);
}
public override string ToString()
{
return _name;
}
}
若转载,请附上本文链接。