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

我刚刚开始一个新的 WPF 应用程序。我有一个网格,想要动态创建行(例如按按钮),然后在该行内创建 TextView/ProgressBar。


我已经搜索过如何以编程方式创建网格行。但在每个解决方案中,我都无法访问里面的内容,并且它变得毫无用处。


<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);

    }

我无法访问以前创建的元素。


回答后:


    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);

    }

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


交互式爱情
浏览 106回答 2
2回答

明月笑刀无情

您可以将所有创建的 StackPanel 存储在列表中。private void AddLineButton_Click(object sender, RoutedEventArgs e){&nbsp; &nbsp; Create_line();}List<StackPanel> items;private void Create_line(){&nbsp; &nbsp; RowDefinition gridRow = new RowDefinition();&nbsp; &nbsp; gridRow.Height = new GridLength(1, GridUnitType.Star);&nbsp; &nbsp; beGrid.RowDefinitions.Add(gridRow);&nbsp; &nbsp; StackPanel stack = new StackPanel();&nbsp; &nbsp; stack.Orientation = Orientation.Horizontal;&nbsp; &nbsp; int i = items.Count + 1;&nbsp; &nbsp; TextBlock textBlock = new TextBlock();&nbsp; &nbsp; textBlock.Text = "Question";&nbsp; &nbsp; textBlock.Name = "Test" + i.ToString();&nbsp; &nbsp; stack.Children.Add(textBlock);&nbsp; &nbsp; beGrid.Children.Add(stack);&nbsp; &nbsp; Grid.SetRow(stack, items.Count);&nbsp; &nbsp; items.Add(stack);}您可以通过索引访问任何以前的面板,例如items[0],并从Children属性获取元素:items[0].Children[0] as TextBlock

倚天杖

像这样手动创建控件确实不是 WPF 方式......最好的方法是定义一个项目类,其中包含要显示/编辑的每个值的属性。然后在窗口中创建ObservableCollection这些项目(因为您将在单击按钮时手动添加项目),并将其设置为控件ItemsSource的属性ItemsControl。ADataTemplate用于定义精确的控件以显示控件中的每个项目,该控件将绑定到该项目的属性。
打开App,查看更多内容
随时随地看视频慕课网APP