My goal is to allow the user to hit the tab key and the autocompletebox to immediately select the first item in the dropdown list. Problem is I can't seem to figure out how to access the first filtered item. Any ideas?
At first I thought the property IsTextCompletionEnabled = true would solve my problem. But if I use FilterMode = Contains, the textcompletion feature doesn't help because nothing is completed.
The ItemsSource doesn't actually change. I need to get to the first item in the filtered list not the itemssource. Is there a way?
Here is my code:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<Recording> MyMusic = new List<Recording>();
MyMusic.Add(new Recording("Chris Sells", "Chris Sells Live", new DateTime(2008, 3, 5)));
MyMusic.Add(new Recording("Chris Sells", "CSells Tells", new DateTime(2004, 4, 6)));
MyMusic.Add(new Recording("Luka Abrus", "The Road to Redmond", new DateTime(2007, 8, 3)));
MyMusic.Add(new Recording("Luka Abrus", "CLuka", new DateTime(2005, 12, 8)));
MyMusic.Add(new Recording("Luka Abrus", "CLove and Luka", new DateTime(2005, 12, 8)));
MyMusic.Add(new Recording("Jim Hance", "The Best of Jim Hance", new DateTime(2007, 2, 6)));
Records.ItemsSource = MyMusic;
}
private void Records_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Tab)
Records.SelectedItem = ((IEnumerable<object>)Records.ItemsSource).First();
}
}
public class Recording
{
public Recording() { }
public Recording(string artistName, string cdName, DateTime release)
{
Artist = artistName;
Name = cdName;
ReleaseDate = release;
}
public string Artist { get; set; }
public string Name { get; set; }
public DateTime ReleaseDate { get; set; }
}
And here is the xaml:
<Controls:AutoCompleteBox x:Name="Records" HorizontalAlignment="Left" Margin="164,142,0,0" VerticalAlignment="Top" Width="182"
ValueMemberPath="Name" KeyDown="Records_KeyDown"
FilterMode="Contains"
IsTextCompletionEnabled="True">
<Controls:AutoCompleteBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</Controls:AutoCompleteBox.ItemTemplate>
</Controls:AutoCompleteBox>