For the wpf listview , in the Mouse Over event how do i get a reference to the item that the mouse cursor is on ?
Regards,
MadSeb
You have to use the MouseOver event from the listViewItem that the mouse is over, not the one from the listview itself.
public MainWindow() {
InitializeComponent();
ListView listView = new ListView();
ListViewItem listViewItem = new ListViewItem();
listViewItem.MouseMove += myMouseMoveEvent;
listView.Items.Add(listViewItem);
}
private void myMouseMoveEvent(object sender, MouseEventArgs e) {
ListViewItem item = (ListViewItem) sender;
// now you can handle the events with this item....
}
Related
In the mouseup event for a listview, how do I get the screen position of a selected item? I can get the screen position of the listview itself (.pointtoscreen) but can't find a way to determine the screen position of a selected item.
I've reviewed other SO articles but didn't find anything specific to items in the listview.
You can handle the ListBox.SelectionChanged event (or Selector.Selected). Then get the container of the selected item to calculate its coordinates:
partial class MainWindow : Window
{
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var listBox = sender as ListBox;
var selectedItemContainer = listBox.ItemContainerGenerator.ContainerFromItem(listBox.SelectedItem) as UIElement;
var pointOnSelectedItem = new Point(); // top-left corner
var pointOnScreen = selectedItemContainer.PointToScreen(pointOnSelectedItem);
var pointRelativeToWindow = selectedItemContainer.TranslatePoint(pointOnSelectedItem, this);
}
}
How to have android like scrollbars for my WPF ListView. I mean to say, the scrollbar should be visible only when the user scrolls. They should fade out as soon as the user stops scrolling. How to achieve this?
You can use the DispatcherTimer class from the System.Windows.Threading namespace.
Here is the XAML markup for your window:
<Window x:Class="testwpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ListView Name="list" ScrollViewer.VerticalScrollBarVisibility="Hidden" Loaded="list_Loaded">
</ListView>
</Window>
And here is the code behind:
public partial class MainWindow : Window
{
DispatcherTimer dispatcherTimer;
public MainWindow()
{
InitializeComponent();
dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += dispatcherTimer_Tick;
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
// we are here only if for the last 500 ms there were no changes of
// the scrollbar's vertical offset and we can hide a scrollbar
list.SetValue(ScrollViewer.VerticalScrollBarVisibilityProperty, ScrollBarVisibility.Hidden);
dispatcherTimer.Stop();
}
private void list_Loaded(object sender, RoutedEventArgs e)
{
// attach a handler
Decorator border = VisualTreeHelper.GetChild(list, 0) as Decorator;
ScrollViewer scroll = border.Child as ScrollViewer;
scroll.ScrollChanged += list_ScrollChanged;
}
private void list_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
// when vertical scroll position is changed, we set VerticalScrollBarVisibilityProperty
// to Auto (scroll bar is displayed only if needed) and run a timer with 500 ms interval
if (e.VerticalChange != 0)
{
list.SetValue(ScrollViewer.VerticalScrollBarVisibilityProperty, ScrollBarVisibility.Auto);
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 500);
dispatcherTimer.Start();
}
}
}
This is a raw example. Of course, you can apply your own conditions defining whether you should hide a scrollbar. For example, you might decide not to hide a scrollbar if left mouse button is still pressed over a scrollbar.
A ListBox and a ContextMenu are created dynamicaly. The ListBox has some items.
How do I know the ListBoxItem Text that right mouse button clicked on?
private void Init2()
{
ContextMenu contextMenu = new ContextMenu();
MenuItem menuItemOpen = new MenuItem();
menuItemOpen.Click += new RoutedEventHandler(menuItemOpen_Click);
contextMenu.Items.Add(menuItemOpen);
listBox1.ContextMenu = contextMenu;
}
void menuItemOpen_Click(object sender, RoutedEventArgs e)
{
//How do I know the listItem text that right mouse button clicked on?
}
When you right click, you actually also select. So that means you can just do:
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
string selectedListBoxItemText = ((ListBoxItem)listBox1.SelectedItem).Content.ToString());
// do your thing
}
I've a listbox in a userControl and I want to select the first item in the listbox whenever datacontext of my userControl is changed. (listbox's ItemsSource is Bind to userControl dataContext :
<userControl>
<ListBox Name="listBox_Resources" ItemsSource="{Binding Path=Resources}" DataContextChanged="listBox_Resources_DataContextChanged">
</ListBox>
</userControl>
private void listBox_Resources_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
MessageBox.Show(listBox_Resources.SelectedIndex.ToString() + " " + listBox_Resources.Items.Count.ToString());
listBox_Resources.SelectedIndex = 0;
}
it seems that dataContextChanged is fired before the listbox items is populated because my messagebox in the eventhandler will return me count of the previous listbox items.
please help me finding the solution.
thanks
Try this
private void listBox_Resources_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
EventHandler eventHandler = null;
eventHandler = new EventHandler(delegate
{
if (listBox_Resources.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
{
listBox_Resources.SelectedIndex = 0;
listBox_Resources.ItemContainerGenerator.StatusChanged -= eventHandler;
}
});
listBox_Resources.ItemContainerGenerator.StatusChanged += eventHandler;
}
If you put a breakpoint at the last line
listBox_Resources.ItemContainerGenerator.StatusChanged += eventHandler;
and watch the value of listBox_Resources.ItemContainerGenerator.Status in the debugger it will should read "ContainersGenerated". If you then add a breakpoint within the delegate EventHanler at
if (listBox_Resources.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
you should see that "c_listBox.ItemContainerGenerator.Status = GeneratingContainers" a first time and then when it hits again it should be ContainersGenerated and then we can set SelectedIndex. Anyway, that's working for me.
How can focus the controls when select a tabitem in a tabcontrol?
You should capture the Selection changed event of the TabControl and inside that you focus the control you need. Just as
private void TabControl1_SelectionChanged(object sender, EventArgs e)
{
control1.Focus();
}
Not sure I understand what you're asking completely, but you probably want to capture the SelectionChanged event for the TabControl:
public Window1()
{
InitializeComponent();
TabControl1.SelectionChanged += TabControl1_SelectionChanged;
}
private void TabControl1_SelectionChanged(object sender, EventArgs e)
{
TabItem item = (TabItem)TabControl1.SelectedItem;
// Find the first control in the TabItem content and focus it?
}