我從數據庫拉項目到一個列表框,它由幾列組成。我想刪除按鈕添加到我的DB拉每個項目,但我似乎無法轉發該項目的ID,它總是說0WPF棱鏡列表框按鈕
<ListBox ItemsSource="{Binding LbPlugins}" HorizontalContentAlignment="Stretch" Grid.Row="1">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*"/>
<ColumnDefinition Width="5*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<CheckBox Grid.Column="0" Content="{Binding Name}" IsChecked="{Binding IsActive}"/>
<Label Grid.Column="1" Content="{Binding ClassName}"/>
<Button Grid.Column="2" Content="E" Command="{Binding btnEditPluginCommand}"/>
<Button Grid.Column="3" Content="D" Command="{Binding Path=DataContext.btnDeletePluginCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}" CommandParameter="{Binding PluginId}"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
在視圖模型:
private int pluginId;
public int PluginId
{
get { return pluginId; }
set { SetProperty(ref pluginId, value); }
}
public DelegateCommand btnDeletePluginCommand { get; set; }
...
在構造
btnDeletePluginCommand = new DelegateCommand(DeletePlugin);
...
private void DeletePlugin()
{
var result = MessageBox.Show("Are you sure you want to delete this plugin?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
MessageBox.Show("YAY, ID=" + pluginId);
}
}