template example wpf binding listbox listboxitem

example - wpf listview binding



Haga doble clic en un elemento ListBox para abrir un navegador (4)

He actualizado la solución AndrewS para resolver el problema con la ejecución del comando haciendo doble clic en cualquier lugar del cuadro de lista:

public class ControlDoubleClick : DependencyObject { public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(ControlDoubleClick), new PropertyMetadata(OnChangedCommand)); public static ICommand GetCommand(Control target) { return (ICommand)target.GetValue(CommandProperty); } public static void SetCommand(Control target, ICommand value) { target.SetValue(CommandProperty, value); } private static void OnChangedCommand(DependencyObject d, DependencyPropertyChangedEventArgs e) { Control control = d as Control; control.PreviewMouseDoubleClick += new MouseButtonEventHandler(Element_PreviewMouseDoubleClick); } private static void Element_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e) { Control control = sender as Control; ICommand command = GetCommand(control); if (command.CanExecute(null)) { command.Execute(null); e.Handled = true; } } }

Y en el XAML la declaración para el ListBox es:

<ListBox ItemsSource="{Binding MyItemsSource, Mode=OneWay}"> <ListBox.ItemContainerStyle> <Style> <Setter Property="behaviours:ControlDoubleClick.Command" Value="{Binding DataContext.MyCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"/> </Style> </ListBox.ItemContainerStyle> </ListBox>

Tengo un ListBox en mi ventana ObervableCollection que se une a un ObervableCollection . Quiero abrir el navegador si alguien hace clic en un elemento de ListBox (como un enlace). alguien puede decirme cómo hacer esto? Encontré algo con listboxviews, ¿funciona solo de esta manera o hay alguna forma de usar ListBox ?

Tuya

Sebastian


Puede agregar un estilo a ListBox.ItemContainerStyle y agregar allí un EventSetter :

<ListBox> .... <ListBox.ItemContainerStyle> <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}"> <EventSetter Event="MouseDoubleClick" Handler="ListBoxItem_MouseDoubleClick"/> </Style> </ListBox.ItemContainerStyle> </ListBox>

ListBoxItem_MouseDoubleClick es un método en su código detrás con la firma correcta para MouseDoubleClick .


Quería resolver esto sin necesidad de manejar el evento double click listBoxItem en el código subyacente, y no quería tener que anular el estilo listBoxItem (o definir el estilo para sobrescribir en primer lugar). Quería simplemente disparar un comando cuando el listBox estaba doblemente marcado.

Creé una propiedad adjunta como tal (el código es muy específico, pero puedes generalizarlo si es necesario):

public class ControlItemDoubleClick : DependencyObject { public ControlItemDoubleClick() { } public static readonly DependencyProperty ItemsDoubleClickProperty = DependencyProperty.RegisterAttached("ItemsDoubleClick", typeof(bool), typeof(Binding)); public static void SetItemsDoubleClick(ItemsControl element, bool value) { element.SetValue(ItemsDoubleClickProperty, value); if (value) { element.PreviewMouseDoubleClick += new MouseButtonEventHandler(element_PreviewMouseDoubleClick); } } static void element_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e) { ItemsControl control = sender as ItemsControl; foreach (InputBinding b in control.InputBindings) { if (!(b is MouseBinding)) { continue; } if (b.Gesture != null && b.Gesture is MouseGesture && ((MouseGesture)b.Gesture).MouseAction == MouseAction.LeftDoubleClick && b.Command.CanExecute(null)) { b.Command.Execute(null); e.Handled = true; } } } public static bool GetItemsDoubleClick(ItemsControl element) { return (bool)element.GetValue(ItemsDoubleClickProperty); }

}

Luego declaro mi ListBox con la propiedad adjunta y mi comando objetivo:

<ListBox ItemsSource="{Binding SomeItems}" myStuff:ControlItemDoubleClick.ItemsDoubleClick="true"> <ListBox.InputBindings> <MouseBinding MouseAction="LeftDoubleClick" Command="MyCommand"/> </ListBox.InputBindings> </ListBox>

Espero que esto ayude.


Utilicé Expression SDK 4.0

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" <i:Interaction.Triggers> <i:EventTrigger EventName="MouseDoubleClick" SourceName="CaravanasListBox"> <i:InvokeCommandAction Command="{Binding AccionesToolbarCommand}" CommandParameter="{x:Static local:OpcionesBarra.MostrarDetalle}" /> </i:EventTrigger> </i:Interaction.Triggers>

Jaimir G.