2009-08-28 10 views
8

क्या एक्सएएमएल आइटमों के गुणों में से किसी एक पर आधारित बाउंड आइटम्स (व्यूमोडेल ऑब्जेक्ट की सूची) आइटम कंट्रोल को स्वचालित रूप से सॉर्ट करने का एकमात्र तरीका है। आइटम्स कंट्रोल डेटा टेम्पलेट का हिस्सा है। मैंने सोचा कि CollectionViewSource चाल करेगा, लेकिन मैं ContentCiewSource को आइटम्स कंट्रोल में कैसे बांधूं। follwoing कोड dispays कुछ भी नहीं:डेटा टेम्पलेट (केवल एक्सएएमएल) में एक बाउंड आइटम्स कंट्रोल को सॉर्ट करना

<--xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"--> 
    <DataTemplate DataType="{x:Type vm:Company}"> 
     <DataTemplate.Resources> 
      <CollectionViewSource x:Key="viewSource" Source="{Binding Employees}"> 
       <CollectionViewSource.SortDescriptions> 
         <scm:SortDescription PropertyName="ID" /> 
        </CollectionViewSource.SortDescriptions> 
      </CollectionViewSource> 
     </DataTemplate.Resources> 
     <Viewbox> 
      <ItemsControl ItemsSource="{Binding Source={StaticResource viewSource}}"> 
       <ItemsControl.ItemsPanel> 
        <ItemsPanelTemplate> 
         <StackPanel Orientation="Horizontal"/> 
        </ItemsPanelTemplate> 
       </ItemsControl.ItemsPanel> 
      </ItemsControl> 
     </Viewbox> 
    </DataTemplate> 
+0

"नियोक्ता" के साथ क्या करने के लिए कुछ भी गलत वर्तनी है? अन्यथा मुझे ठीक लग रहा है। – Crispy

+0

नहीं, ऐसा नहीं है कि यहां समस्या (हो सकता है) कि ViewModel बाध्यकारी ({x: टाइप vm: company}) ज्ञात नहीं है या रिसोर्स स्कोप के भीतर मूल्यांकन नहीं किया गया है। कर्मचारी कंपनी बीटीडब्ल्यू की एक संपत्ति है। – bitbonk

उत्तर

20

सीधे DataTemplateViewbox के दायरे को CollectionViewSource संसाधन चलती बजाय का प्रयास करें:

<DataTemplate DataType="{x:Type vm:Company}"> 
    <Viewbox> 
     <Viewbox.Resources> 
      <CollectionViewSource x:Key="viewSource" Source="{Binding Employees}"> 
       <CollectionViewSource.SortDescriptions> 
         <scm:SortDescription PropertyName="ID" /> 
        </CollectionViewSource.SortDescriptions> 
      </CollectionViewSource> 
     </Viewbox.Resources> 
     <ItemsControl ItemsSource="{Binding Source={StaticResource viewSource}}"> 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate> 
        <StackPanel Orientation="Horizontal"/> 
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 
     </ItemsControl> 
    </Viewbox> 
</DataTemplate> 
+3

उत्तर के विस्तार के रूप में, कारण यह है कि डेटा टेम्पलेट के रूट तत्व में यह डेटाकॉन्टेक्स्ट सेट है। डेटा टेम्पलेट स्वयं नहीं करता है। चूंकि डेटाकॉन्टेक्स्ट टेम्पलेट ऑब्जेक्ट से जुड़ने का एकमात्र तरीका है, इसलिए आपको संसाधन को गैर-नल डेटा कॉन्टेक्स्ट के दायरे में रखना होगा। – Gusdor

5

मैं एक DataTemplate या ViewBox यह करने के लिए उपयोग नहीं किया। आप

<ItemsControl xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase" 
     x:Name="MyItemsControl" Loaded="MyItemsControl_Loaded"> 
    <ItemsControl.ItemTemplate> 
    <DataTemplate> 
     <ItemsControl> 
     <ItemsControl.Resources> 
      <CollectionViewSource x:Key="Orders" Source="{Binding Orders}"> 
      <CollectionViewSource.SortDescriptions> 
       <scm:SortDescription PropertyName="OrderID" Direction="Ascending"/> 
      </CollectionViewSource.SortDescriptions> 
      </CollectionViewSource> 
     </ItemsControl.Resources> 
     <ItemsControl.ItemsSource> 
      <Binding Source="{StaticResource Orders}"/> 
     </ItemsControl.ItemsSource> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
      <StackPanel Orientation="Horizontal"/> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
      <TextBlock Text="{Binding OrderID}"/> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
     </ItemsControl> 
    </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

गुड लक एक ItemsControl.Resource निर्दिष्ट द्वारा सॉर्ट क्रम चुन सकते हैं ....!

संबंधित मुद्दे