[英]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.