【发布时间】: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 模式。然后只需从绑定集合中删除绑定项目。容易。