【问题标题】:how to remove dynamically created Row in user control in WPF?如何在 WPF 的用户控件中删除动态创建的行?
【发布时间】:2014-06-20 16:43:20
【问题描述】:

我创建了一个用户控件。我正在页面中动态创建此用户控件。我在该控件中有一个按钮来删除动态创建的行。我无法删除整行。 这是我的 Xaml:

 <Grid Name="grid_usercontrolTypeofFixture" >
    <Grid.RowDefinitions >
        <RowDefinition Height="20*"/>
        <RowDefinition Height="35*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="35*" />
        <ColumnDefinition Width="50*" />            
        <ColumnDefinition Width="15*" />
        <ColumnDefinition Width="10*" />
    </Grid.ColumnDefinitions>
    <Label Grid.Row="0"  Grid.Column="0" Margin="10,5,5,5" Content="Select/Type Fixture Type:" />      

    <TextBox Grid.Row="0" Grid.Column="1" Margin="10,5,5,5" Text="" Name="txtAuto"/>
    <TextBox Grid.Row="0" Grid.Column="2" Margin="10,5,5,5" Text="" Name="txt_percentage"/>

    <Button Grid.Row="0" Grid.Column="3" Margin="10,5,5,5" x:Name="btn_removeRow" Content="" Click="btn_removeRow_Click">
        <Button.Background>
            <ImageBrush ImageSource="/Assets/Knob Cancel.png"></ImageBrush>
        </Button.Background>
    </Button>
 </Grid>

这是我的代码:

 public int count = 1;
 private void btn_addnew_Click(object sender, RoutedEventArgs e)
    {
        var myControl = new UserControlTypeofFixture() { Name = "TypeofFixture" };
        grid_typeFixture.RowDefinitions.Add(new RowDefinition());
        Grid.SetRow(myControl, count);
        grid_typeFixture.Margin = new Thickness(10, 10, 10, 10);
        grid_typeFixture.Children.Add(myControl);
        count++;
    }

  private void btn_removeRow_Click(object sender, RoutedEventArgs e)
    {

               ???            
    }

我还有一些其他代码可以删除选定的行。但它不工作。我怎样才能删除整行?

【问题讨论】:

  • 您好,您的问题已在以下位置得到解答:WPF Delete Row from grid stackoverflow.com/questions/20860193/wpf-delete-row-from-grid。另外,我建议作为折叠行而不是删除它的替代方法(将行高设置为 0)。 Rgds,亚历克斯
  • 它不是用户控件..
  • 我建议您像其他使用 WPF 的人一样使用 MVVM 模式。然后只需从绑定集合中删除绑定项目。容易。

标签: c# wpf xaml


【解决方案1】:

这是删除usercontrolrowdefinition 的方法

private void btn_removeRow_Click(object sender, RoutedEventArgs e)
        {

            var myControl = sender as Button;
            int rowindex = (int)myControl.GetValue(Grid.RowProperty);

            foreach (UIElement control in grid_typeFixture.Children)
            {
                var usercontrol = control as UserControlTypeofFixture;
                if (usercontrol != null)
                {
                    int childrowindex = (int)usercontrol.GetValue(Grid.RowProperty);
                    if (childrowindex == rowindex)
                    {
                        grid_typeFixture.Children.Remove(control);
                        grid_typeFixture.RowDefinitions.RemoveAt(childrowindex);
                        break;
                    }
                }

            }

        }

【讨论】:

    【解决方案2】:

    好的,然后尝试我提到的替代解决方案,即将特定的 RowDefinition Height 设置为 0:与您的情况相关,它应该如下所示:

     private void btn_removeRow_Click(object sender, RoutedEventArgs e)
     {
        grid_typeFixture.RowDefinitions[2].Height = new GridLength(0);            
     }
    

    【讨论】:

      猜你喜欢
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      • 2014-04-08
      • 2010-12-16
      • 1970-01-01
      • 1970-01-01
      • 2011-01-02
      相关资源
      最近更新 更多