2012-08-29 13 views
5

उदाहरण के लिए मैं कुछ इस तरह कर सकता है:डब्ल्यूपीएफ में, क्या ग्रिड जैसे गठबंधन कॉलम के साथ स्टैकपैनल बनाने का कोई तरीका है?

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*" /> 
     <ColumnDefinition Width="Auto" /> 
     <ColumnDefinition Width="Auto" /> 
     <ColumnDefinition Width="Auto" /> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition Width="Auto"> 
     <RowDefinition Width="Auto"> 
     <RowDefinition Width="Auto"> 
    </Grid.RowDefinitions> 
    <TextBlock Grid.Row="0" Grid.Column="0">Header 1</TextBlock> 
    <TextBox Grid.Row="0" Grid.Column="1" MaxLines="1" /> 
    <Button Grid.Row="0" Grid.Column="2">Send</Button> 
    <Button Grid.Row="0" Grid.Column="3">Save</Button> 
    <TextBlock Grid.Row="1" Grid.Column="0">Header 2</TextBlock> 
    <TextBox Grid.Row="1" Grid.Column="1" MaxLines="1" /> 
    <Button Grid.Row="1" Grid.Column="2">Send</Button> 
    <Button Grid.Row="1" Grid.Column="3">Save</Button> 
    <TextBlock Grid.Row="2" Grid.Column="0">Header 3</TextBlock> 
    <TextBox Grid.Row="2" Grid.Column="1" MaxLines="1" /> 
    <Button Grid.Row="2" Grid.Column="2">Send</Button> 
    <Button Grid.Row="2" Grid.Column="3">Save</Button> 
</Grid> 

या मैं कुछ इस तरह कर सकता है:

<StackPanel> 
    <StackPanel Orientation="Horizontal"> 
     <TextBlock>Header 1</TextBlock> 
     <TextBox MaxLines="1" /> 
     <Button>Send</Button> 
     <Button>Save</Button> 
    </StackPanel> 
    <StackPanel Orientation="Horizontal"> 
     <TextBlock>Header 2</TextBlock> 
     <TextBox MaxLines="1" /> 
     <Button>Send</Button> 
     <Button>Save</Button> 
    </StackPanel> 
    <StackPanel Orientation="Horizontal"> 
     <TextBlock>Header 3</TextBlock> 
     <TextBox MaxLines="1" /> 
     <Button>Send</Button> 
     <Button>Save</Button> 
    </StackPanel> 
<StackPanel> 

छोड़कर कि मैं आसानी से पंक्तियों (नई पंक्तियाँ जोड़ने हेरफेर करने के लिए सक्षम होना चाहते हैं, चारों ओर पंक्तियों को ले जाएं, इत्यादि) बस स्टैकपैनल की तरह, कॉलम को ठीक से ग्रिड की तरह गठबंधन रखते हुए रखते हुए।

उत्तर

3

आप कॉलम पर size sharing के साथ कई एक-पंक्ति Grids का उपयोग कर सकते हैं। बिना किसी नियंत्रण के किए जाने पर वर्बोज़ हो जाता है, इसलिए आप ItemsControl या attached property के व्युत्पन्न में कुछ तर्क (जैसे कॉलम बनाने और Grid.Column असाइन करना) को समाहित कर सकते हैं।

4

यहां एक वर्ग है जिसे मैंने लगभग एक ही उद्देश्य के लिए एक साथ फेंक दिया - अनिवार्य रूप से मैं बाएं कॉलम में लेबल का एक गुच्छा और दाईं ओर विभिन्न प्रकार के टेक्स्ट (टेक्स्ट बॉक्स, ड्रॉपडाउन इत्यादि) को रखने में सक्षम होना चाहता था।

इसमें केवल दो कॉलम हैं, लेकिन इसे विभिन्न संख्याओं में अनुकूलित किया जा सकता है।

public class LabelValueGrid : Grid 
{ 
    public LabelValueGrid() 
     : base() 
    { 
     ColumnDefinitions.Add(new ColumnDefinition()); 
     ColumnDefinitions.Add(new ColumnDefinition()); 
     ColumnDefinitions[0].Width = new System.Windows.GridLength(1, System.Windows.GridUnitType.Auto); 
     ColumnDefinitions[1].Width = new System.Windows.GridLength(1, System.Windows.GridUnitType.Star); 
    } 

    protected override void OnVisualChildrenChanged(System.Windows.DependencyObject visualAdded, System.Windows.DependencyObject visualRemoved) 
    { 
     base.OnVisualChildrenChanged(visualAdded, visualRemoved); 

     int curRow = -1; 
     int curCol = 1; 

     RowDefinitions.Clear(); 

     if (Children != null) 
      foreach (System.Windows.UIElement curChild in Children) 
      { 
       if (curCol == 0) 
        curCol = 1; 
       else 
       { 
        curCol = 0; 
        curRow++; 
        RowDefinitions.Add(new RowDefinition() {Height = new System.Windows.GridLength(1, System.Windows.GridUnitType.Auto)}); 
       } 

       Grid.SetRow(curChild, curRow); 
       Grid.SetColumn(curChild, curCol); 
      } 

     RowDefinitions.Add(new RowDefinition() {Height = new System.Windows.GridLength(1, System.Windows.GridUnitType.Star)}); 
    } 
} 

मैं एक ग्रिड की तरह उपयोग कर सकते हैं, सिवाय इसके कि मैं सिर्फ नियंत्रण सूची और फिर इसे कॉलम को स्वचालित रूप alternates:

<local:LabelValueGrid> 
    <TextBlock Text="Label1"/> 
    <TextBox Name="value1"/> 
    <TextBlock Text="Label2"/> 
    <TextBox Name="value2"/> 
</local:LabelValueGrid> 
+0

बस मस्ती के लिए, [यहाँ UWP के लिए एक संस्करण है] (https://pastebin.com/2nstKFrC) इस कोड के आधार पर ठीक काम करने लगता है। मैं यूडब्ल्यूपी-भूमि में अभी तक 'ऑनविज़ुअल चिल्ड्रेन चेंजेड' के बराबर नहीं पाया गया है, हालांकि, यह केवल एक तरीका है जिसे आप स्पष्ट रूप से कॉल करते हैं जो 'IEnumerable ' लेता है। – ruffin

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

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