समस्याWPF ListBox वर्चुअलाइजेशन शिकंजा अप आइटम
हम कुशलतापूर्वक एक WPF ListBox नियंत्रण में वस्तुओं की एक बड़ी (> 1000) संख्या को प्रदर्शित करने की आवश्यकता है का प्रदर्शन किया। हम इन वस्तुओं को कुशलता से प्रदर्शित करने के लिए WPF ListBox के वर्चुअलाइजेशन (वर्चुअलाइजिंगस्टैक पैनेल के माध्यम से) पर भरोसा कर रहे हैं।
बग: वर्चुअलाइजेशन का उपयोग करते समय WPF ListBox नियंत्रण आइटम को सही तरीके से प्रदर्शित नहीं करता है।
पुन कैसे
हम नीचे दिखाया गया स्टैंडअलोन XAML के लिए समस्या आसुत है।
xaml को XAMLPad में कॉपी और पेस्ट करें।
प्रारंभ में, सूची बॉक्स में कोई चयनित आइटम नहीं है, इसलिए उम्मीद है कि सभी आइटम एक ही आकार के हैं और वे पूरी तरह से उपलब्ध स्थान को भरते हैं।
अब, पहले आइटम पर क्लिक करें। जैसा कि अपेक्षित है, हमारे डेटा टेम्पलेट की वजह से, चयनित आइटम अतिरिक्त जानकारी दिखाने के लिए विस्तारित होगा।
जैसा कि अपेक्षित है, यह क्षैतिज स्क्रॉलबार प्रकट होने का कारण बनता है, क्योंकि चयनित आइटम अब उपलब्ध स्थान से अधिक व्यापक है।
अब क्षैतिज स्क्रॉलबार को दाईं ओर क्लिक करने और खींचने के लिए माउस का उपयोग करें।
बग: गैर-चयनित दृश्य आइटम अब उपलब्ध स्थान को भरने के लिए नहीं फैले हैं। सभी दृश्यमान वस्तुओं को एक ही चौड़ाई होना चाहिए।
क्या यह ज्ञात बग है? क्या यह ठीक करने के लिए कोई तरीका है, या तो XAML या प्रोग्रामेटिक रूप से?
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<Page.Resources>
<DataTemplate x:Key="MyGroupItemTemplate">
<Border Background="White"
TextElement.Foreground="Black"
BorderThickness="1"
BorderBrush="Black"
CornerRadius="10,10,10,10"
Cursor="Hand"
Padding="5,5,5,5"
Margin="2"
>
<StackPanel>
<TextBlock Text="{Binding Path=Text, FallbackValue=[Content]}" />
<TextBlock x:Name="_details" Visibility="Collapsed" Margin="0,10,0,10" Text="[xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx]" />
</StackPanel>
</Border>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}"
Value="True">
<Setter Property="TextElement.FontWeight"
TargetName="_details"
Value="Bold"/>
<Setter Property="Visibility"
TargetName="_details"
Value="Visible"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</Page.Resources>
<DockPanel x:Name="LayoutRoot">
<Slider x:Name="_slider"
DockPanel.Dock="Bottom"
Value="{Binding FontSize, ElementName=_list, Mode=TwoWay}"
Maximum="100"
ToolTip="Font Size"
AutoToolTipPlacement="BottomRight"/>
<!--
I want the items in this ListBox to completly fill the available space.
Therefore, I set HorizontalContentAlignment="Stretch".
By default, the WPF ListBox control uses a VirtualizingStackPanel.
This makes it possible to view large numbers of items efficiently.
You can turn on/off this feature by setting the ScrollViewer.CanContentScroll to "True"/"False".
Bug: when virtualization is enabled (ScrollViewer.CanContentScroll="True"), the unselected
ListBox items will no longer stretch to fill the available horizontal space.
The only workaround is to disable virtualization (ScrollViewer.CanContentScroll="False").
-->
<ListBox x:Name="_list"
ScrollViewer.CanContentScroll="True"
Background="Gray"
Foreground="White"
IsSynchronizedWithCurrentItem="True"
TextElement.FontSize="28"
HorizontalContentAlignment="Stretch"
ItemTemplate="{DynamicResource MyGroupItemTemplate}">
<TextBlock Text="[1] This is item 1." />
<TextBlock Text="[2] This is item 2." />
<TextBlock Text="[3] This is item 3." />
<TextBlock Text="[4] This is item 4." />
<TextBlock Text="[5] This is item 5." />
<TextBlock Text="[6] This is item 6." />
<TextBlock Text="[7] This is item 7." />
<TextBlock Text="[8] This is item 8." />
<TextBlock Text="[9] This is item 9." />
<TextBlock Text="[10] This is item 10." />
</ListBox>
</DockPanel>
</Page>
धन्यवाद विल! अधिक जानकारी के लिए नीचे "उत्तर" देखें। –