2009-02-16 76 views
3

我能想到的唯一例子是在html中 - 如果你動態地添加一個TR w/a colspan + div它包含細節(可編輯)點擊前面的TR例如如何在WPF中動態添加詳細信息行?

我試圖Grok XAML,並想看看是否有人可以指出我這個古怪的請求正確的方向。

回答

7

這是什麼東西,不知道這是否是你在找什麼:

XAML:

<Grid Name="_mainGrid"> 
    <Grid.ColumnDefinitions> 
     <!-- Contains the button --> 
     <ColumnDefinition Width="Auto"/> 
     <!-- Contains the edit control --> 
     <ColumnDefinition Width="*"/> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <!-- So that we have the 'empty' space at the end --> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 
</Grid> 

代碼:

public Window1() 
    { 
     InitializeComponent(); 
     CreateRow(); // Bootstrap 
    } 

    private void CreateRow() 
    { 
     RowDefinition newRow = new RowDefinition(); 
     newRow.Height = new GridLength(0, GridUnitType.Auto); 
     _mainGrid.RowDefinitions.Insert(_mainGrid.RowDefinitions.Count - 1, newRow); 

     int rowIndex = _mainGrid.RowDefinitions.Count - 2; 

     UIElement editControl = CreateEditControl(); 
     Grid.SetRow(editControl, rowIndex); 
     Grid.SetColumn(editControl, 1); 
     Grid.SetRowSpan(editControl, 1); 
     Grid.SetColumnSpan(editControl, 1); // Change this if you want. 
     _mainGrid.Children.Add(editControl); 

     Button addButton = new Button(); 
     addButton.Content = "Add"; 
     addButton.Click += new RoutedEventHandler(b_Click); 
     Grid.SetRow(addButton, rowIndex); 
     Grid.SetColumn(addButton, 0); 
     _mainGrid.Children.Add(addButton); 
     addButton.Tag = editControl; 

    } 

    void b_Click(object sender, RoutedEventArgs e) 
    { 
     CreateRow(); 
     Control button = (Control)sender; 
     UIElement editControl = (UIElement)button.Tag; 
     _mainGrid.Children.Remove(button); 
     Grid.SetColumn(editControl, 0); 
     Grid.SetColumnSpan(editControl, 2); 
    } 

    private UIElement CreateEditControl() 
    { 
     return new TextBox(); 
    } 
+0

讓我工作起來快速POC測試此代碼,當我看到有用的東西時我會接受答案;) – 2009-02-17 13:33:14