WPF ListBox ListBoxItem Binding

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.

+4
source share
2 answers

What is the actual value returned by a Path called SelectedItem?

This is System.Windows.Controls.ListBoxItem:15 (you can read it as "ListBoxItem with content set to 15"), so your binding does not work - it expects a numeric value, not ListBoxItem . You can specify Path as SelectedItem.Content to make this work. You can also set the SelectedValuePath in the SizeBox to "Content" and bind to the SelectedValue property instead of SelectedItem .

Solution 1:

 <TextBlock Size="{Binding ElementName=Size, Path=SelectedItem.Content}" /> 

Solution 2:

 <ListBox x:Name="Size" SelectedValuePath="Content" /> <TextBlock Size="{Binding ElementName=Size, Path=SelectedValue}" /> 

Why does the FontFamily SelectItem function work?

Since this ListBox contains a collection of fonts, not a collection of ListBoxItems (they are still created to represent each item in the collection, though). You can achieve the same behavior with font sizes if you define a collection of font sizes in the code and bind the ListBox'es ItemsSource property to this collection or define the contents of your ListBox as a set of System.Double values ​​directly in XAML:

 <ListBox x:Name="Size" xmlns:system="clr-namespace:System;assembly=mscorlib"> <system:Double>10</system:Double> <system:Double>15</system:Double> <system:Double>20</system:Double> </ListBox> 
+2
source

1) The actual value returned by the SelectedItem binding is a ListBoxItem object. To get the value (15) from your binding, you can use a converter or make your binding path more explicit to get the value of the content list item.

 Size="{Binding ElementName=Size, Path=SelectedItem.Content}" 

2) This is a covariant operation, so the type of each list item is derived from its source. Elements created by a font family element control (ListBox) are the result of collection bindings. The Items property (populated through the ItemsSource dependency property) is an ItemCollection of shared objects that accept the type of the corresponding contextual objects.

+1
source

Source: https://habr.com/ru/post/1402023/


All Articles