I go through the Sams book, Learn Yourself in WPF in 24 Hours. At some point, the authors show how you can bind the value of the selected ListBox to a property. I understand this is pretty simple. But when I try to create my own ListBox control with my own ListBoxItems, I cannot get it to work.
The ListBox that works uses the system collection as the ItemsSource property:
<ListBox x:Name="FontList" DockPanel.Dock="Left" ItemsSource="{x:Static Fonts.SystemFontFamilies}" Width="160" />
The value selected from this list is then used in the TextBlock as follows:
<TextBlock Text="Test" FontFamily="{Binding ElementName=FontList, Path=SelectedItem}" TextWrapping="Wrap" Margin="0 0 0 4" />
Note that Path is set to SelectedItem.
Now I wanted to install FontSize using another ListBox that contains 3 different sizes. Here is what I did:
<ListBox x:Name="Size" > <ListBoxItem>10</ListBoxItem> <ListBoxItem>15</ListBoxItem> <ListBoxItem>20</ListBoxItem> </ListBox>
And then I added a binding to the Size attribute of the TextBox as follows:
<TextBlock Text="Test" FontFamily="{Binding ElementName=FontList, Path=SelectedItem}" Size="{Binding ElementName=Size, Path=SelectedItem}" TextWrapping="Wrap" Margin="0 0 0 4" />
The file size does not change when the program starts. So I tried to add the snapping that I used for the Size attribute to Text to see its value:
<TextBlock Text="{Binding ElementName=Size, Path=SelectedItem}"" FontFamily="{Binding ElementName=FontList, Path=SelectedItem}" Size="{Binding ElementName=Size, Path=SelectedItem}" TextWrapping="Wrap" Margin="0 0 0 4" />
I see that it changes when I click on the ListBox, but I also see that the SelectedItem displays like this (when I click the 15 button): System.Windows.Controls.ListBoxItem: 15
My questions are: 1) What is the actual value returned by Path called SelectedItem? Is it "System.Windows.Controls.ListBoxItem: 15" or is it "15"? If it's not 15, how can I specify a path that returns only 15, and not System.Windows.Controls.ListBoxItem: 15?
2) Why does the FontFamily SelectItem function work? I understand that the FontList comes from the System font set of names, but I donβt understand why the ListBox does not return the ListBoxItems collection as text. If the reference to the ListBox Path returns a SelectedItem object of type ListBoxItem, I would think that I could use Path of SelectedItem.Value or something like that, but that does not work, and Intellisense does not help me.
I want this example to work, because it will help clarify some of the misunderstandings that I have. Please do not refactor the solution to make it work in any other way, unless it is completely impossible for me to have a link to Path, which will give me only the digital part of my ListBoxItem.