我有一个绑定到数据网格的列表。 xml datagrid代码是:
<DataGrid x:Name="Mydg" Margin="5" CanUserAddRows="False" IsReadOnly="True" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Host IP" Binding="{Binding Ip}"></DataGridTextColumn>
<DataGridTextColumn Header="Host Name" Binding="{Binding Name}"></DataGridTextColumn>
<DataGridTextColumn Header="Port" Binding="{Binding Port}"></DataGridTextColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock>
<Hyperlink Name="hlRmv" Click="HlRmv_OnClick">Remove</Hyperlink>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
C#:
...
Mydg.ItemsSource=psList;
...
在“HlRmv_OnClick”中如何删除点击的行?
答案 0 :(得分:0)
在这一行
<Hyperlink Name="hlRmv" Click="HlRmv_OnClick">Remove</Hyperlink>
你可以像这样添加标签
<Hyperlink Name="hlRmv" Click="HlRmv_OnClick" Tag="{Binding Ip}">Remove</Hyperlink>
在点击时,您将在点击事件
中获得Tag形式的发件人参数 private void HlRmv_OnClick(object sender, RoutedEventArgs e)
{
var link = sender as Hyperlink;
var Ip = link.Tag as string;
// your logic of removing the code here. Reload collection
var result = psList.Where(item => item.Ip.Equals(Ip));
psList.Remove(result);
}