Ted jsem se trochu zasekl na vytvareni ToolTipu na kazde polozce ListBoxu, a na scesti me privedli i nektere rady na webu, ale o tom az nize. Cely kod je tentokrat velice primitivni. Prvne si vytvorime ListBox, se sablonou, ktera bude zobrazovat TextBlock a nad nim ToolTip. Data budeme brat pomoci Data Bindingu:
1 2 3 4 5 6 7 8 9 10 11 |
<ListBox x:Name="lbSongs" Height="160" MaxHeight="160" Margin="0" Padding="0"> <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Path=FileName}"> <ToolTipService.ToolTip> <TextBlock Text="{Binding Path=DirectoryName}"> </TextBlock> </ToolTipService.ToolTip> </TextBlock> </DataTemplate> </ListBox.ItemTemplate> </ListBox> |
Problem s nekterymi clanky na webu byl ten ze jsem chtel pouzit primo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
using System.Windows.Controls; using System.Windows.Documents; class Song { public Song(string fn) { string p, s; FS.GetPathAndFileName(fn, out p, out s); FileName = s; DirectoryName = p; } public string FileName { get ; set ; } public string DirectoryName { get ; set ; } public override string ToString() { return FileName; } } |
Poslednim krokem je naplneni ListBoxu, to samozrejme provedeme v Code-Behindu:
1 2 3 4 5 6 7 8 9 10 11 12 |
public RightToolTipApproachForCustomObjects() { InitializeComponent(); PopulateListBox(); } void PopulateListBox() { List<Song> files3 = new List<Song>(); files3.Add(new Song(@"C:\Users\sunamo\Desktop\Dolcezza 1.mp3")); files3.Add(new Song(@"C:\Users\sunamo\Desktop\Dolcezza 2.mp3")); lbSongs.ItemsSource = files3; } |
Finalni vysledek muze pak vypadat nejak takto: