双击ListBox项以打开浏览器

Seb*_*ler 41 wpf binding listbox listboxitem

ListBox在我的wpf窗口中绑定了一个ObervableCollection.如果有人点击了某个元素ListBox(就像链接一样),我想打开浏览器.谁能告诉我怎么做?我发现了listboxviews的一些东西,它只是以这种方式工作还是有一种方法只是使用ListBox

你的

塞巴斯蒂安

Bob*_*ing 82

您可以向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>
Run Code Online (Sandbox Code Playgroud)

ListBoxItem_MouseDoubleClick是代码中的一个方法,具有MouseDoubleClick的正确签名.

  • @Bob +1为缺少双击的简单解决方案. (2认同)

And*_*ewS 10

我想解决这个问题,而不需要在代码隐藏中处理listBoxItem双击事件,我不想重写listBoxItem样式(或者首先定义要覆盖的样式).我想在listBox被双击时发出一个命令.

我创建了一个像这样的附加属性(代码非常具体,但您可以根据需要进行概括):

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);
}
Run Code Online (Sandbox Code Playgroud)

}

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

<ListBox ItemsSource="{Binding SomeItems}"
     myStuff:ControlItemDoubleClick.ItemsDoubleClick="true">
<ListBox.InputBindings>
    <MouseBinding MouseAction="LeftDoubleClick" Command="MyCommand"/>
</ListBox.InputBindings>
</ListBox>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.


Vad*_*fan 6

我已经更新了AndrewS解决方案,以解决在单击列表框中的任意位置时触发执行命令的问题:

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在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>
Run Code Online (Sandbox Code Playgroud)


use*_*813 5

我使用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>
Run Code Online (Sandbox Code Playgroud)

贾米尔