简体   繁体   English

如何动态创建和修改新的Grid行元素?

[英]How to dynamically create and modify a new Grid row elements?

I'm just starting a new WPF app. 我刚刚启动一个新的WPF应用程序。 I have a grid and want to create the rows dynamically (pressing a button for example) and then create TextView/ProgressBar inside this row. 我有一个网格,想要动态创建行(例如,按下按钮),然后在此行内创建TextView / ProgressBar。

I already searched how to create the gridrows programatically. 我已经搜索过如何以编程方式创建网格行。 But in every solution, i can't access what's inside and it becomes useless. 但是在每种解决方案中,我都无法访问其中的内容,它变得毫无用处。

<Grid x:Name="MainGrid">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Button x:Name="AddLineButton" Content="Click to add a new line" Click="AddLineButton_Click"/>
    <Grid x:Name="beGrid" Grid.Row="1">
<!-- I need my new rows here -->
    </Grid>
</Grid>
int i = 0; //nb of rows

    private void AddLineButton_Click(object sender, RoutedEventArgs e)
    {
        Create_line();
        i++;
    }

    private void Create_line()
    {
        RowDefinition gridRow = new RowDefinition();
        gridRow.Height = new GridLength(1, GridUnitType.Star);
        beGrid.RowDefinitions.Add(gridRow);
        StackPanel stack = new StackPanel();
        stack.Orientation = Orientation.Horizontal;
        TextBlock textBlock = new TextBlock();
        textBlock.Text = "Question";
        textBlock.Name = "Test" + i.ToString();
        stack.Children.Add(textBlock);
        beGrid.Children.Add(stack);
        Grid.SetRow(stack, i);
    }

I can't access a previously created element. 我无法访问先前创建的元素。

AFTER ANSWER : 回答后:

    private void Create_line()
    {
        RowDefinition gridRow = new RowDefinition();
        gridRow.Height = new GridLength(1, GridUnitType.Star);
        beGrid.RowDefinitions.Add(gridRow);
        StackPanel stack = new StackPanel();
        stack.Orientation = Orientation.Horizontal;
        TextBlock textBlock = new TextBlock();
        textBlock.Text = "Question";
        textBlock.Name = "Test" + i.ToString();
        RegisterName(textBlock.Name, textBlock);
        stack.Children.Add(textBlock);
        beGrid.Children.Add(stack);
        Grid.SetRow(stack, i);
    }

To get the created TextBlock : var text = (TextBlock)FindName("Test"+i.ToString()); 要获取创建的TextBlock: var text = (TextBlock)FindName("Test"+i.ToString());

you can store all created StackPanel in a List. 您可以将所有创建的StackPanel存储在一个列表中。

private void AddLineButton_Click(object sender, RoutedEventArgs e)
{
    Create_line();
}

List<StackPanel> items;

private void Create_line()
{
    RowDefinition gridRow = new RowDefinition();
    gridRow.Height = new GridLength(1, GridUnitType.Star);
    beGrid.RowDefinitions.Add(gridRow);

    StackPanel stack = new StackPanel();
    stack.Orientation = Orientation.Horizontal;

    int i = items.Count + 1;
    TextBlock textBlock = new TextBlock();
    textBlock.Text = "Question";
    textBlock.Name = "Test" + i.ToString();

    stack.Children.Add(textBlock);
    beGrid.Children.Add(stack);
    Grid.SetRow(stack, items.Count);

    items.Add(stack);
}

you can access any previos panel by index, eg items[0] , and get elements from Children property: items[0].Children[0] as TextBlock 您可以按索引访问任何previos面板,例如items[0] ,并从Children属性中获取元素: items[0].Children[0] as TextBlock

Creating controls manually like this is really not the WPF way ... 像这样手动创建控件实际上不是WPF方式...

The best methodology is to define an item class that holds properties for each value that you want to display / edit. 最好的方法是定义一个项目类,其中包含要显示/编辑的每个值的属性。

Then create an ObservableCollection (since you will be manually adding items on a button click) of these items within your Window, and set this as the ItemsSource property of an ItemsControl control. 然后在Window中创建这些项目的ObservableCollection (因为您将通过单击按钮手动添加项目),并将其设置为ItemsControl控件的ItemsSource属性。 A DataTemplate is used to define the exact controls to display each item within the control, which will bind to the properties of the item. DataTemplate用于定义确切的控件以显示控件中的每个项目,这些控件将绑定到项目的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM