簡體   English   中英

在wpf中動態刪除網格中的列

[英]dynamically removing columns from grid in wpf

我有一個數字1到10的組合框,根據選擇用戶,我的表單上應該顯示許多用戶控件。第一次表單加載組合框的值為1,網格中只有一個用戶控件petdetails (只有一行,並且在進行更改時添加了其他列)。 我的程序工作正常,直到這個程度,但一旦用戶添加更高並減少由於某種原因列刪除但最后一列中的控件重疊。(例如:用戶選擇6,6個控件添加和一次他將組合框中的值更改為3,只剩下3列,但在第三列中,我可以看到用戶控件5,6經過重疊)。我在這里粘貼我的代碼,任何幫助都將非常感謝。

private void cmbNoOfPets_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        int i = Convert.ToInt32(((System.Windows.Controls.ContentControl)(cmbNoOfPets.SelectedValue)).Content);
        int grdCols=Convert.ToInt32(grdPetDetails .ColumnDefinitions .Count.ToString ()); 
        // check if the number of pets is more than the existing number in the grid if more start with adding at the next column
        // if less start from the last decreasing till the number of columns required
            if (i > grdCols)
            for (int j = grdCols+1 ; j <= i; j++)
            {

                ColumnDefinition c = new ColumnDefinition();
                c.Width = new GridLength(370, GridUnitType.Pixel);
                grdPetDetails.ColumnDefinitions.Add  (c);
                PetDetails petdetails = new PetDetails();
                petdetails.Name = "petDetails" + j ;
                string str="Pet Number " + j + ":";
                petdetails.lblPetNumber.Content = str;
                Grid.SetRow(petdetails, 1);
                Grid.SetColumn(petdetails, j - 1);
                grdPetDetails.Children.Add(petdetails);

           }
            else if (i < grdCols)

                for (int j = grdCols; j > i; j--)
                {

                    PetDetails petdetails = new PetDetails();
                    petdetails.Name = "petDetails" + j;
                    grdPetDetails.Children.Remove(petdetails);
                    grdPetDetails.ColumnDefinitions.RemoveAt(j - 1);


                } 

     }

我首先刪除控件然后刪除列定義,但后來無法理解為什么會發生這種情況。

提前致謝。

因此,重新解釋問題/請求“當我更改組合框的SelectedItem時,我希望新的SelectedIndex + 1控件數量顯示在應用程序的其他位置。” 看起來很簡單,只需將事件處理程序連接到SelectionChanged,然后在該事件處理程序中創建控件。 http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.selectionchanged.aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM