使用ListBox和Button删除记录

时间:2015-11-28 09:54:14

标签: c# wpf

我需要在ListBox中显示数据库中的所有记录,并在ListBox中添加按钮,以便在wpf应用程序中使用c#从数据库中删除该特定记录。
我用过...但无法在记录前的Button中添加ListBox

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    Button b = new Button();
    b.Content = "myitem";
    b.Click += new RoutedEventHandler(b_Click);
    listboxEmployee.Items.Add(b);
}
void b_Click(object sender, RoutedEventArgs e)
{
   //code
}

和XAML:

<ListView Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="4" Grid.RowSpan="4" Name="listboxEmployee" SelectionChanged="listboxEmployee_SelectionChanged"> 
    <ListView.View> 
        <GridView> 
            <GridViewColumn> 
            </GridViewColumn>
        </GridView>
     </ListView.View>
</ListView>

1 个答案:

答案 0 :(得分:0)

试试这个:

<GridView>
   <GridViewColumn>
      <GridViewColumn.CellTemplate>
           <DataTemplate>
               <Button Name="btnDelete"  Content="Delete" Click="BtnDelete_OnClick" />
           </DataTemplate>
      </GridViewColumn.CellTemplate>
   </GridViewColumn>
</GridView>

然后:

private void BtnDelete_OnClick(object sender, RoutedEventArgs e)
{
    var s = (((sender as Button).DataContext) as YourModel).Property;
    //Write Delete code based on "s" value
}