2012-05-13 43 views
6

我有一個網格。我必須手動定義每列和行,像這樣:如何在不定義每行的情況下設置網格列/行大小?

<Window x:Class="GridBuild" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="GridBuild" Height="300" Width="300"> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition/> 
     <RowDefinition/> 
    </Grid.RowDefinitions>   
    <Grid.ColumnDefinitions> 
     <ColumnDefinition/> 
     <ColumnDefinition/> 
    </Grid.ColumnDefinitions>   
</Grid> 

我想定義的行和列的數目與單行線,這樣的事情:

<Window x:Class="GridBuild" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="GridBuild" Height="300" Width="300"> 
    <Grid> 
     <Grid.NumberOfRows="2"/> 
     <Grid.NumberOfColumns/> 
    </Grid> 
</Window> 

回答

4

我建議從Grid派生出來,並在其中添加如下屬性:

public class GridEx : Grid 
{ 
    public int NumberOfRows 
    { 
     get { return RowDefinitions.Count; } 
     set 
     { 
      RowDefinitions.Clear(); 
      for (int i = 0; i < value; i++) 
       RowDefinitions.Add(new RowDefinition()); 
     } 
    } 

    public int NumberOfColumns 
    { 
     get { return ColumnDefinitions.Count; } 
     set 
     { 
      ColumnDefinitions.Clear(); 
      for (int i = 0; i < value; i++) 
       ColumnDefinitions.Add(new ColumnDefinition()); 
     } 
    } 
} 

現在,它可以被用作如下:

<local:GridEx NumberOfRows="3" NumberOfColumns="2"> 
    <TextBox>some text</TextBox> 
    <TextBox Grid.Row="1">some text</TextBox> 
    <TextBox Grid.Row="2">some text</TextBox> 

    <TextBox Grid.Column="1">some text</TextBox> 
    <TextBox Grid.Row="1" Grid.Column="1">some text</TextBox> 
    <TextBox Grid.Row="2" Grid.Column="1">some text</TextBox> 
</local:GridEx> 

工程在設計以及順便說一下:)

這裏的挑戰是如何設置不同的寬度,高度等,爲不同的行和列。我有一個很好的想法如何做到這一點。也可以自動分配Grid.Row和Grid.Column。想想:)

7

你所描述的是UniformGrid。它具有ColumnsRows屬性,通過它可以設置所需的行數或列數。

如果您沒有設置這些屬性,UniformGrid會嘗試儘可能將孩子佈局爲儘可能靠近正方形。在這種情況下,它希望在增加行數之前增加列數。

這是一個晦澀難懂的面板,但它正確使用時功能非常強大。

3

EvAlex的上述答案將起作用,但前提是您不希望使用數據綁定來設置列/行的數量。

public class GridEx : Grid 
{ 
    public int NumberOfRows 
    { 
     get { return RowDefinitions.Count; } 
     set 
     { 
      RowDefinitions.Clear(); 
      for (int i = 0; i < value; i++) 
       RowDefinitions.Add(new RowDefinition()); 
     } 
    } 

    public int NumberOfColumns 
    { 
     get { return ColumnDefinitions.Count; } 
     set 
     { 
      ColumnDefinitions.Clear(); 
      for (int i = 0; i < value; i++) 
       ColumnDefinitions.Add(new ColumnDefinition()); 
     } 
    } 
} 

如果你想通過數據綁定設置這些(像我一樣),然後用上述溶液,編譯器會抱怨,因爲它需要DependencyProperties了點。一個DependencyProperty可以實現(使用C#6的nameof運營商)如下(一種快速插入它使用的是片段propdp):

public int Columns 
{ 
    get { return (int) GetValue(ColumnsDependencyProperty); } 
    set { SetValue(ColumnsDependencyProperty, value); } 
} 
public static readonly DependencyProperty ColumnsDependencyProperty = 
    DependencyProperty.Register(nameof(Columns), typeof(int), typeof(GridEx), new PropertyMetadata(0)); 

不過這樣一來,你不能執行必要的邏輯來添加必要的號碼RowDefinitions。要解決這個問題,請爲每個DependencyProperty定義一個DependencyPropertyDescriptor,並在您的自定義類的構造函數中添加一個調用AddValueChanged所需的邏輯。然後每個propery的結果是(使用C#6的空條件運算符?.):

public int Columns 
    { 
     get { return (int) GetValue(ColumnsDependencyProperty); } 
     set { SetValue(ColumnsDependencyProperty, value); } 
    } 
    public static readonly DependencyProperty ColumnsDependencyProperty = 
     DependencyProperty.Register(nameof(Columns), typeof(int), typeof(GridEx), new PropertyMetadata(0)); 

    DependencyPropertyDescriptor ColumnsPropertyDescriptor = DependencyPropertyDescriptor.FromProperty(ColumnsDependencyProperty, typeof(GridEx)); 

    public GridEx() 
    { 
     ColumnsPropertyDescriptor?.AddValueChanged(this, delegate 
     { 
      ColumnDefinitions.Clear(); 
      for (int i = 0; i < Columns; i++) 
       ColumnDefinitions.Add(new ColumnDefinition()); 
     }); 
    } 
相關問題