简体   繁体   English

如何从网格控件中删除选定的行

[英]How can I remove selected row from a grid control

I have created a Grid and adding rows dynamically.我创建了一个网格并动态添加行。 And I have a remove button in every row.我在每一行都有一个删除按钮。 On click of that remove button I want to remove that particular row.单击该删除按钮时,我想删除该特定行。

I am quite new to WPF, can some one help me out to achieve this.我对 WPF 很陌生,有人可以帮我实现这一目标吗?

Thanks.谢谢。

Xaml code: Xaml 代码:

   <Grid Grid.Column="2"  Visibility="Collapsed" Name="operationalGrid">
       <Button Height="25" Name="btnRemove" Width="24" Style="{DynamicResource ButtonStyle}" Grid.Column="4" Click="btnRemove_Click">
            <materialDesign:PackIcon Background="Transparent" Height="25" Width="30" Cursor="Hand" ToolTip="Remove" Kind="Minus" HorizontalAlignment="Center" Foreground="White"  VerticalAlignment="Top" Margin="0,0,0,0">
            </materialDesign:PackIcon>
       </Button>
   </Grid>

c# code: c# 代码:

Adding rows:添加行:

   RowDefinition row = new RowDefinition();
   row.Height = GridLength.Auto;
   if (ucDynamicControls != null)
       ucDynamicControls.btnToggle.Visibility = ucDynamicControls.btnToggle.Visibility = Visibility.Visible;
       ucDynamicControls = new ucControls();
       grdContols.RowDefinitions.Add(row);
       Grid.SetRow(ucDynamicControls, controlCount++);
       grdContols.Children.Add(ucDynamicControls);  

图片

Instead of using row definitions, you may use ItemsControl.您可以使用 ItemsControl,而不是使用行定义。

As example:例如:

First define your ItemTemplate首先定义您的 ItemTemplate

<ItemsControl Name="icTodoList">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid Margin="0,0,0,5">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="100" />
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding Title}" />
                <ProgressBar Grid.Column="1" Minimum="0" Maximum="100" Value="{Binding Completion}" />
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Define your ItemsPanel定义您的 ItemsPanel

<ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <UniformGrid Columns="3" />
    </ItemsPanelTemplate>
</ItemsControl.ItemsPanel>

Full tutorial: https://www.wpf-tutorial.com/list-controls/itemscontrol/完整教程: https://www.wpf-tutorial.com/list-controls/itemscontrol/

You have come up with a very complex, one might say, perverse way of accomplishing the task.你想出了一种非常复杂的、有人可能会说是不正当的完成任务的方法。 A typical implementation is to create a container element with data.一个典型的实现是用数据创建一个容器元素。 Then the collection with these elements is passed to ItemsControl.然后将包含这些元素的集合传递给 ItemsControl。 And he already visual each element as it should.他已经将每个元素都可视化了。 In this implementation: deleting a row is removing an item from the collection.在这个实现中:删除一行就是从集合中删除一个项目。

In your implementation, you need to parse the Visual Element Tree.在您的实现中,您需要解析可视元素树。 It is possible, but difficult.这是可能的,但很难。 The button, as I understand it, is inside the Grid row.据我了解,该按钮位于 Grid 行内。 This Grid is inside ucDynamicControls.这个网格在 ucDynamicControls 里面。 And the outer Grid row already contains this ucDynamicControls.并且外部 Grid 行已经包含这个 ucDynamicControls。

If I understood correctly, then in the button event it is necessary to go up from the button to the Grid containing ucDynamicControls.如果我理解正确,那么在按钮事件中必须将 go 从按钮向上移动到包含 ucDynamicControls 的 Grid。 In this Grid, remove the ucDynamicControls element from the Children collection.在此 Grid 中,从 Children 集合中删除 ucDynamicControls 元素。 And the line (in which it) is to collapse: Heigh = 0.而这条线(它所在的线)将塌陷:Heigh = 0。

And it's not clear why you chose Grid, and StackPanel for the ucDynamicControls collection?并且不清楚您为什么选择 Grid 和 StackPanel 作为 ucDynamicControls 集合? If you use a StackPanel, then it will be possible not to set the line number and for it will be enough just to remove the desired ucDynamicControls from the collection.如果您使用 StackPanel,则可以不设置行号,只需从集合中删除所需的 ucDynamicControls 就足够了。

An example implementation (if I understand your code correctly):一个示例实现(如果我正确理解您的代码):

    private void btnRemove_Click(object sender, RoutedEventArgs e)
    {
        if (!(sender is Button button))
            return;

        DependencyObject child = button;
        DependencyObject parent;
        while (!((parent = LogicalTreeHelper.GetParent(child)) is UcDynamicControls) && parent != null)
            child = parent;

        if (parent == null)
            return;

        UcDynamicControls ucDynamicControls = (UcDynamicControls)parent;
        int row = Grid.GetRow(ucDynamicControls);

        Grid grid = LogicalTreeHelper.GetParent(ucDynamicControls) as Grid;

        if (grid == null || grid.RowDefinitions.Count <= row)
            return;

        grid.RowDefinitions[row].Height = new GridLength(0);;
        grid.Children.Remove(ucDynamicControls);

    }

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

相关问题 如何从一个网格视图传输和删除单个选定的行到另一个网格视图 - how can i transfer and remove single selected row from one grid view to another 从数据网格中删除选定的行 - Remove selected row from data grid 如何从所选添加的控件中获取Grid.Row Grid.Column? - How to get the Grid.Row Grid.Column from the selected added control? 使用服务器绑定时,如何从Kendo网格选择的行中获取数据? - How can I get the data from a Kendo grid selected row when using server binding? 如何通过C#在运行时从网格视图控件中删除文本参数 - How can i remove the text parameter from grid view control on runtime through C# 如何通过网格行和列号选择用户控件? - How can I select a user control by grid row and column number? 如何从 DataGridView 控件的底部删除空行? - How do I remove the empty row from the bottom of a DataGridView control? 如何从网格的某个位置获取和删除控件 - How to get and remove the control at a position from the Grid 当选择一个项目时,如何将控件添加到GridView中,使得该控件位于所选项目下方的行上并跨越所有列? - How can I add a control to a GridView when an item is selected such that the control is on the row below the selected item and spans all the columns? 如何通过lambda从网格中删除子UIElement? - How can I remove Child UIElement from a Grid by lambda?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM