2010-06-28 14 views
25

के लिए अभी भी WPF के साथ चारों ओर बेवकूफ बनाना और जैसा कि मैंने जाना सीखने के रूप में। नियंत्रण की एक गतिशील समूह बनाने के लिए अब कोशिश कर रहा है (ज्यादातर बटन लेकिन चेकबॉक्स और अन्य शामिल हो सकते हैं)।WrapPanel ItemPanel ItemsControl

मुझे नहीं पता कि क्या सबसे अच्छा तरीका यह है तो मैं एक ItemsControl शैली बनाने की कोशिश की और फिर एक WrapPanel अंदर एक ItemsPresenter में आइटम जोड़ने के लिए था। जल्द ही एहसास हुआ कि आइटम लपेट नहीं पाएंगे क्योंकि वे प्रभावी रूप से WrapPanel के अंदर नहीं थे जब तक कि मैं इसे आइटमहोस्ट के रूप में नहीं डालता। इस तरह:

<Style x:Key="ButtonPanelGroup" TargetType="{x:Type ItemsControl}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ItemsControl}"> 
       <Border CornerRadius="5" 
         BorderBrush="{StaticResource DarkColorBrush}" 
         BorderThickness="1" 
         Margin="5"> 
        <Grid> 
         <Grid.RowDefinitions> 
          <RowDefinition /> 
          <RowDefinition Height="Auto" /> 
         </Grid.RowDefinitions> 

         <WrapPanel IsItemsHost="True" FlowDirection="LeftToRight"> 
          <ItemsPresenter /> 
         </WrapPanel> 

         <ContentPresenter ContentSource="Name" Grid.Row="1" /> 

        </Grid> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

ध्यान दें कि यह एक काम प्रगति पर है और अभी भी कई स्टाइल प्रभाव हैं जिन्हें मुझे अभी भी लागू करने की आवश्यकता है। यहाँ मैं इसका इस्तेमाल:

<UniformGrid Rows="1"> 
    <ItemsControl Name="Group1" Style="{StaticResource ButtonPanelGroup}"> 
     <Button>Button1</Button> 
     <Button>Button2</Button> 
     <CheckBox>TickBox</CheckBox> 
    </ItemsControl> 

    <ItemsControl Name="Group2" Style="{StaticResource ButtonPanelGroup}"> 
     <Button>Button3</Button> 
     <Button>Button4</Button> 
     <Button>Button5</Button> 
    </ItemsControl> 

    <ItemsControl Name="Group3" Style="{StaticResource ButtonPanelGroup}"> 
     <Button>Button6</Button> 
     <Button>Button7</Button> 
     <Button>Button8</Button> 
    </ItemsControl> 
</UniformGrid> 

इसके अलावा यहाँ ध्यान दें कि यह अभी भी कार्य प्रगति पर है के रूप में UniformGrid यहां जाने का रास्ता नहीं होगा और यह भी मार्जिन एक दर्द हो सकता है (वहाँ किसी भी मार्जिन जो आपस में मिलते हैं?) तो इनपुट की सराहना की जाएगी।

अब वास्तविक समस्या को

। यह मैं एक त्रुटि मिलती है काम नहीं करता:

'ItemsPresenter' object cannot be added to 'WrapPanel'. Cannot explicitly modify Children collection of Panel used as ItemsPanel for ItemsControl. ItemsControl generates child elements for Panel. Error at object 'System.Windows.Controls.ItemsPresenter'.

तो क्या इस तरह कुछ करने के लिए सबसे अच्छा तरीका है (बस ItemControl में बटन और अन्य नियंत्रण फेंकने के लिए सक्षम होने के लिए प्यार होता है और लाइन अप वास्तविक अच्छा) । क्या नियंत्रण को किसी तरह के संग्रह और पुनरावृत्त में रखना बेहतर होगा।

यह अच्छी तरह मिल किया करने के लिए, लेकिन मेरी WPF कौशल अभी भी कमी है प्यार करोगे। क्या कोई डब्ल्यूपीएफ किताबें हैं जो मूल बातें से परे पढ़ती हैं और दिखाती हैं कि समर्थक वास्तव में ऐसा कैसे करेगा?

उत्तर

39

आप ItemsPanel संपत्ति पर एक नज़र लेने के लिए चाहते हो सकता है:

Gets or sets the template that defines the panel that controls the layout of items.

उदाहरण:

<ItemsControl> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <WrapPanel /> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
</ItemsControl> 

और आप एक शैली में सेट कर सकते हैं इस प्रकार है:

<Style TargetType="ItemsControl"> 
    <Setter Property="ItemsPanel"> 
     <Setter.Value> 
      <ItemsPanelTemplate> 
       <WrapPanel /> 
      </ItemsPanelTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
+0

यह काम करता है लेकिन मैं इसे प्रत्येक आइटम नियंत्रण के बजाय शैली में सेट नहीं करना चाहता था। मैंने सोचा कि IsItemsHost संपत्ति के साथ संभव था लेकिन काम नहीं लग रहा है। क्या यह एकमात्र तरीका है? –

+0

आइटम्स कंट्रोल – Arcturus

+0

के लिए शैली के साथ अद्यतन उत्तर मैं निश्चित रूप से इस शैली में भी जोड़ सकता हूं, धन्यवाद। यदि आपके पास समय है तो आप 2 अन्य प्रश्न देख सकते हैं जो अभी भी अनसुलझे हैं। http://stackoverflow.com/questions/3118266/wpf-datatemplate-property-set-at-content http://stackoverflow.com/questions/3004967/how-to-reuse-layouts-in-wpf –

3

सुराग संपत्ति IsItemsHost = "सच" की परिभाषा को मत भूलना। अन्यथा आपके आइटम नियंत्रण आपके आइटम नहीं दिखाएंगे।

<ListBox ItemsSource="{Binding MyItemsSource}"> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate > 
       <WrapPanel IsItemsHost="True"/> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
    </ListBox> 
संबंधित मुद्दे