双击列表框项目以打开浏览器

我有一个ListBox在我的wpf窗口绑定到ObervableCollection 。 我想打开浏览器,如果有人点击ListBox一个元素(就像一个链接)。 有人能告诉我如何做到这一点? 我用listboxviews发现了一些东西,它只是用这种方式,或者只是使用ListBox吗?

你的

塞巴斯蒂安

您可以将样式添加到ListBox.ItemContainerStyle ,并在其中添加一个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是您的代码中的方法,具有正确的MouseDoubleClick签名。

我想解决这个问题,而不需要处理代码隐藏中的listBoxItem双击事件,我不希望重写listBoxItem样式(或定义样式来重写第一个地方)。 我想在listBox被双击时激发一个命令。

我创build了一个像这样的附加属性(代码是非常具体的,但你可以按需要概括):

 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); } 

}

然后我用附加属性和我的目标命令声明我的ListBox:

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

希望这可以帮助。

我使用了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.

我已经更新了AndrewS解决scheme,以便解决问题,如果双击列表框中的任何位置,则执行该命令时会触发该问题:

 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; } } } 

在XAML中,ListBox的声明是:

 <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>