Natürlich :)
Man nehme einmal eine Datenklasse:
namespace DotNetGui.ListBoxHooverSelector
{
public class Person
{
private static int _counter = 0;
public String FirstName { get; set; }
public String LastName { get; set; }
public static Person Build()
{
return new Person() { FirstName = "Test" + (_counter++).ToString(), LastName = "Tester" + (_counter++).ToString() };
}
}
}
Dann definiere man ein Window:
<Window x:Class="DotNetGui.ListBoxHooverSelector.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ListBox Hoover Selector" Height="300" Width="400">
<Window.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="LightGray"/>
<Setter Property="Margin" Value="0 2 0 2"/>
</Style>
<DataTemplate x:Key="PersonTemplate">
<StackPanel Orientation="Horizontal" MouseMove="StackPanel_MouseMove">
<TextBlock Text="{Binding LastName}"/>
<TextBlock Text=", "/>
<TextBlock Text="{Binding FirstName}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border BorderBrush="LightGray" BorderThickness="1" CornerRadius="5" Padding="2" Margin="2" Grid.Column="0">
<ListBox x:Name="PersonListBox" BorderThickness="0" ItemTemplate="{StaticResource PersonTemplate}"/>
</Border>
<StackPanel Orientation="Vertical" Grid.Column="1">
<TextBlock Text="Firstname of selected person"/>
<TextBox Text="{Binding SelectedItem.FirstName, ElementName=PersonListBox}"/>
<TextBlock Text="Lastname of selected person"/>
<TextBox Text="{Binding SelectedItem.LastName, ElementName=PersonListBox}"/>
</StackPanel>
</Grid>
</Window>
Es wird eine ListBox definiert und dann Datenfelder, welche die Informationen aus dem jeweilig ausgewählten Item der ListBox anzeigen. Dazu gibt es noch ein Template für die Items der ListBox als auch einen Style für die TextBox.
Was wir jetzt noch benötigen ist das Auffüllen der ListBox mit Daten und den Hover-Effekt:
namespace DotNetGui.ListBoxHooverSelector
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
PersonListBox.Items.Add(new Person() { LastName = "Eder", FirstName = "Norbert" });
PersonListBox.Items.Add(Person.Build());
PersonListBox.Items.Add(Person.Build());
PersonListBox.Items.Add(Person.Build());
PersonListBox.Items.Add(Person.Build());
PersonListBox.Items.Add(Person.Build());
PersonListBox.Items.Add(Person.Build());
PersonListBox.Items.Add(Person.Build());
}
private void StackPanel_MouseMove(object sender, MouseEventArgs e)
{
PersonListBox.SelectedItem = (sender as StackPanel).DataContext;
}
}
}
Fertig.
Download: Beispielanwendung