سؤال

ولدي ListBox في إطار برنامج الأغذية العالمي بلدي الذي يربط إلى ObervableCollection. أريد لفتح المتصفح إذا ينقر على عنصر من ListBox (تماما مثل وصلة). هل بإمكان احد اخباري بكيفية عمل هذا؟ لقد وجدت شيئا مع listboxviews، يعمل فقط بهذه الطريقة أم أن هناك وسيلة فقط عن طريق استخدام 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 هو أسلوب في التعليمات البرمجية خلف مع التوقيع الصحيح ل <لأ href = "http://msdn.microsoft.com/en-us/library/system.windows.controls.control.mousedoubleclick.aspx" يختلط = "noreferrer"> MouseDoubleClick .

نصائح أخرى

وكنت أرغب في حل هذا دون الحاجة للتعامل مع listBoxItem الحدث انقر مرتين في التعليمات البرمجية الخلفية، وأنا لا أريد أن يكون لتجاوز أسلوب listBoxItem (أو تحديد أسلوب لتجاوز في المقام الأول). أردت لاطلاق النار فقط أمر عندما doubleclicked مربع القائمة.

وأنا خلقت خاصية تعلق مثل ذلك (رمز محددة جدا، ولكن يمكنك التعميم كما هو مطلوب):

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 ItemsSource="{Binding SomeItems}"
     myStuff:ControlItemDoubleClick.ItemsDoubleClick="true">
<ListBox.InputBindings>
    <MouseBinding MouseAction="LeftDoubleClick" Command="MyCommand"/>
</ListBox.InputBindings>
</ListBox>

وآمل أن يساعد هذا.

ولقد تحديث الحل اندروز من أجل حل هذه المسألة مع إطلاق تنفيذ الأمر إذا النقر المزدوج أي مكان في مربع القائمة:

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

واعتدت التعبير 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.

وفيما يلي السلوك الذي يحصل أن يتم على كلا ListBox وListView. وتستند هذه الردود من أندرو S. وفاديم Tofan، رفاق العمل العظيم!

public class ItemDoubleClickBehavior : Behavior<ListBox>
{
    #region Properties
    MouseButtonEventHandler Handler;
    #endregion

    #region Methods

    protected override void OnAttached()
    {
        base.OnAttached();

        AssociatedObject.PreviewMouseDoubleClick += Handler = (s, e) =>
        {
            e.Handled = true;
            if (!(e.OriginalSource is DependencyObject source)) return;

            ListBoxItem sourceItem = source is ListBoxItem ? (ListBoxItem)source : 
                source.FindParent<ListBoxItem>();

            if (sourceItem == null) return;

            foreach (var binding in AssociatedObject.InputBindings.OfType<MouseBinding>())
            {
                if (binding.MouseAction != MouseAction.LeftDoubleClick) continue;

                ICommand command = binding.Command;
                object parameter = binding.CommandParameter;

                if (command.CanExecute(parameter))
                    command.Execute(parameter);
            }
        };
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.PreviewMouseDoubleClick -= Handler;
    }

    #endregion
}

إليك الطبقة تمديد استخدامها للعثور على الأم.

public static class UIHelper
{
    public static T FindParent<T>(this DependencyObject child, bool debug = false) where T : DependencyObject
    {
        DependencyObject parentObject = VisualTreeHelper.GetParent(child);

        //we've reached the end of the tree
        if (parentObject == null) return null;

        //check if the parent matches the type we're looking for
        if (parentObject is T parent)
            return parent;
        else
            return FindParent<T>(parentObject);
    }
}

والاستعمال:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:coreBehaviors="{{Your Behavior Namespace}}"


<ListView AllowDrop="True" ItemsSource="{Binding Data}">
    <i:Interaction.Behaviors>
       <coreBehaviors:ItemDoubleClickBehavior/>
    </i:Interaction.Behaviors>

    <ListBox.InputBindings>
       <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding YourCommand}"/>
    </ListBox.InputBindings>
</ListView>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top