2012-02-15 23 views
0

我使用網格,即內容將經常改變。 在XAML它只是看起來像這樣:重置並填充網格c#

<Grid Name="grid1"/> 

在C#我這樣做:

private void informGrid() 
{ 
    grid1= new Grid(); 
    grid1.Background = new SolidColorBrush(Colors.Transparent); 
    grid1.Visibility = Visibility.Visible; 

    Canvas.SetZIndex(grid1, 51); 

    grid1.SetValue(Grid.RowProperty, 1); 
    grid1.Margin = new Thickness(0, 0, 0, 0); 

    grid1.RowDefinitions.Add(new RowDefinition { Height = new GridLength(hw) }); 
    grid1.RowDefinitions.Add(new RowDefinition { Height = new GridLength(hw) }); 

    grid1.ColumnDefinitions.Add(new ColumnDefinition{ Width=new GridLength(hw)}); 
    grid1.ColumnDefinitions.Add(new ColumnDefinition{ Width=new GridLength(hw)}); 
    grid1.ColumnDefinitions.Add(new ColumnDefinition{ Width=new GridLength(hw)}); 
    grid1.ColumnDefinitions.Add(new ColumnDefinition{ Width=new GridLength(hw)}); 
    grid1.ColumnDefinitions.Add(new ColumnDefinition{ Width=new GridLength(hw)}); 

    fillGrid(); 
} 

然後我加10張進入這個格子,在每個單元1(100%的工作)。 網格變得可見,有正確的行& columndefinitons,10個孩子,但圖片不存在。

我打賭只是一個小錯誤,但我無法找到它。

請幫幫我。 謝謝。

+0

是什麼在fillGrid? – 2012-02-15 13:20:00

+0

用圖像填充網格的代碼。在不同情況下測試之前,工作100% – roqstr 2012-02-15 13:43:20

+0

'private void fillGrid() { for(int i = 0; i <10; i ++) { Image blank = new Image(); blank.Source =(ImageSource)new ImageSourceConverter()。ConvertFromString(「Images/blank.png」); images.Add(blank);blank.SetValue(Grid.ColumnProperty,j);blank.SetValue(Grid.RowProperty,k); blank.Height = hw; blank.Width = hw; grid1.Children.Add(空白);' – roqstr 2012-02-15 13:45:52

回答

0

使用stackpanels代替,工作得很好,你只需要清除內容

0

你可以嘗試添加圖片,如:

Image yourImage = CreateImage(); //In Create image you return the image you want to display 

grid1.Children.Add(yourImage);