How do you get the Item under the mouse course?



  • Processing MouseMove or PreviewMouseMove♪ Type of container - any ItemsControl♪ As ItemsSource, IEnumerable<MyClass>♪ So when you put a mice on the control element, I need a copy of this myself. MyClasswhich is stored in a container.

    To approach reality, let's have some kind of object object in VM, which will contain a copy of the collection, the container of which is under the mouse cade. Container may be TYPE♪ Even the usual. ItemsControl



  • There are many methods. For example:

    <ListBox ItemsSource="<тут привязываетесь к нужной коллекции>"
             HorizontalContentAlignment="Stretch"
             ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border MouseEnter="OnMouseEnter" MouseLeave="OnMouseLeave"
                        Background="Transparent">
                    <!-- тут отображение вашей VM, замените на то, что нужно -->
                    <ContentPresenter Content="{Binding}"/>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    and code-behind:

    void OnMouseEnter(object sender, MouseEventArgs e)
    {
        var b = (FrameworkElement)sender;
        var item = b.DataContext;
        Debug.WriteLine($"Mouse entered: {item}");
    }
    

    void OnMouseLeave(object sender, MouseEventArgs e)
    {
    var b = (FrameworkElement)sender;
    var item = b.DataContext;
    Debug.WriteLine($"Mouse left: {item}");
    }


    The right method depends on what you need this very element for.


    There's a way through. MouseMove The container itself, but it's long and unspecified.

    In XAML:

    There are many methods. For example:

    <ListBox ItemsSource="<тут привязываетесь к нужной коллекции>"
    HorizontalContentAlignment="Stretch"
    ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListBox.ItemTemplate>
    <DataTemplate>
    <Border MouseEnter="OnMouseEnter" MouseLeave="OnMouseLeave"
    Background="Transparent">
    <!-- тут отображение вашей VM, замените на то, что нужно -->
    <ContentPresenter Content="{Binding}"/>
    </Border>
    </DataTemplate>
    </ListBox.ItemTemplate>
    </ListBox>

    and code-behind:

    void OnMouseEnter(object sender, MouseEventArgs e)
    {
    var b = (FrameworkElement)sender;
    var item = b.DataContext;
    Debug.WriteLine($"Mouse entered: {item}");
    }

    void OnMouseLeave(object sender, MouseEventArgs e)
    {
    var b = (FrameworkElement)sender;
    var item = b.DataContext;
    Debug.WriteLine($"Mouse left: {item}");
    }


    The right method depends on what you need this very element for.


    If you really want to put the processor on the list, you can do that:

    In XAML:

    <ListBox ItemsSource="<тут привязываетесь к нужной коллекции>"
    HorizontalContentAlignment="Stretch"
    ScrollViewer.HorizontalScrollBarVisibility="Disabled"
    MouseMove="OnMouseMove">

    and in code-behind:

    void OnMouseMove(object sender, MouseEventArgs e)
    {
    var b = (ListBox)sender;
    var coordinatesAboutList = e.GetPosition(b);
    var itemInsideContainer = b.InputHitTest(coordinatesAboutList);
    var container = FindContainer(itemInsideContainer, b);
    if (container == null)
    return;
    var item = b.ItemContainerGenerator.ItemFromContainer(container);
    Debug.WriteLine($"Mouse moved over: {item}");
    }

    ListBoxItem FindContainer(IInputElement itemInsideContainer, ListBox b)
    {
    ListBoxItem candidate = null;
    for (DependencyObject currentItem = (DependencyObject)itemInsideContainer;
    currentItem != null;
    currentItem = VisualTreeHelper.GetParent(currentItem))
    {
    candidate = currentItem as ListBoxItem;
    if (candidate != null)
    break;
    }
    if (candidate != null)
    {
    for (DependencyObject parent = (DependencyObject)itemInsideContainer;
    parent != b;
    parent = VisualTreeHelper.GetParent(parent))
    {
    if (parent == null)
    return null;
    }
    }
    return candidate;
    }

    But it's better to get the mouse out. ItemTemplate or ItemContainerStyleI don't have to run through a visual tree looking for the right element.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2