The best way, since you are not very interested in choosing an element (because it will be deleted quickly anyway), would pass the element directly to the command as the CommandParameter parameter.
Alternatively, you can do a workaround using code or triggers, but I don't think it will. For instance:
ButtonBase.Click ,
<ListBox ButtonBase.Click="lb_Click"
...
, :
private void lb_Click(object sender, RoutedEventArgs e)
{
object clicked = (e.OriginalSource as FrameworkElement).DataContext;
var lbi = lb.ItemContainerGenerator.ContainerFromItem(clicked) as ListBoxItem;
lbi.IsSelected = true;
}
, datacontext , ListBoxItem ListBox ItemContainerGenerator IsSelected true. . , ButtonBase .
, ( ), Behavior:
public class SelectItemOnButtonClick : Behavior<ListBox>
{
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.AddHandler(ButtonBase.ClickEvent, new RoutedEventHandler(handler), true);
}
protected override void OnDetaching()
{
this.AssociatedObject.RemoveHandler(ButtonBase.ClickEvent, new RoutedEventHandler(handler));
base.OnDetaching();
}
private void handler(object s, RoutedEventArgs e)
{
object clicked = (e.OriginalSource as FrameworkElement).DataContext;
var lbi = AssociatedObject.ItemContainerGenerator.ContainerFromItem(clicked) as ListBoxItem;
lbi.IsSelected = true;
}
}
:
<ListBox xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" ...>
<i:Interaction.Behaviors>
<local:SelectItemOnButtonClick />
</i:Interaction.Behaviors>
</ListBox>
, , , , , , , .
, Handled true , (MouseDown/Click), ListBoxItem. MouseDown ListBox , ListBoxItem, ... , , , , , FrameworkContentElements ( MouseDown), , , , datatemplate, ListBoxItem , , .
-, , , ( ... err thingies). , , , reaaaly , , . , - , .