scroll down a list box automatically - silverlight

On a grid, i have a ListBox and a button. If the button is clicked, an item is added to the listbox.
The problem i'm trying to fix is that after the item is added, it is not focused.
I want to automatically scroll down the listbox so that an user can see the item that is lately added. Any thought?

You can set the SelectedIndex property to set the currently selected item.
If it scrolls of the page, you can use ScrollIntoView() to keep the bottom of the list showing.
listBox1.SelectedIndex = listBox1.Items.Count;
listBox1.ScrollIntoView(listBox1.SelectedItem);

I had to force the call to ScrollIntoView onto the UI thread and this seemed to do the trick.
Here's an example of this working. A
dd this as the event handler of an application bar icon button click event in a new DataBound application.
private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
App.ViewModel.Items.Add(new ItemViewModel
{
LineOne = "new L1",
LineTwo = "new L2",
LineThree = "new L3"
});
Dispatcher.BeginInvoke(() =>
MainListBox.ScrollIntoView(MainListBox.Items.Last()));
}

Related

wpf get screen position of selected item in listview

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

UIElement.Focus() does not work, why?

My WPF window has a Button and a ListView. On the Button's click event, it has the following code.
private void myButton1_Click(object sender, RoutedEventArgs e)
{
ListViewItem lvi = new ListViewItem()
{
Content = "Hello",
Focusable = true,
IsEnabled = true
};
this.listView1.Items.Add(lvi);
lvi.Focus();
}
Here is the problem, the focus cannot move to lvi(ListViewItem), after the user clicks the Button. The code lvi.Focus() does not have any effect. Can someone tell me why it happens, and how can I solve it?
Update:
Find the solution. call this code, otherwise the focus is still captured by the button.
this.Dispatcher.BeginInvoke(new Action(() => lvi.Focus()), System.Windows.Threading.DispatcherPriority.Input);
Try the following:
ListViewItem lvi = new ListViewItem();
lvi.IsSelected = true;
Hope that helps ;)

User control is not hiding in wpf

I created a UserControl like Popup which is displayed when user clicks on menu item.
If user clicks side that user control should be collapsed.
It works fine for me when user clicks side other than any control.
If I click on datagrid or listbox it is not hiding.
Here is my code:
<src:AddNewItemPopUp x:Name="PopUp" Margin="111,47,620,230" Panel.ZIndex="1" Visibility="Collapsed"/>
I took a button in click event I set PopUp visibility property to true
In my user control I have grid. In the mousedown event of grid I have written following code...
private void Grid_MouseDown_1(object sender, MouseButtonEventArgs e)
{
if (PopUp.Visibility == Visibility.Visible)
{
PopUp.Visibility = Visibility.Collapsed;
}
}
If I click on any control like a Button, DataGrid, ListBox that are placed in Grid Popup is not collapsed.
First Set Grid's Background Property, for example grid.Backgroung=Brushes.Transparent or in Xaml Backgroung = "Transparent"
Second Handle PreviewMouseDown event instead of MouseDown event.
The first one makes the mouse event to fire, when mouse is directly over the grid.
The second one makes the mouse event fire, when mouse is over an UIElement in the grid.
Try:
Visibility="Hidden"
that is:
private void Grid_MouseDown_1(object sender, MouseButtonEventArgs e)
{
if (PopUp.Visibility == Visibility.Visible)
{
PopUp.Visibility = Visibility.Hidden;
}
}
and also see:
http://msdn.microsoft.com/en-us/library/system.windows.visibility.aspx

PopUp StaysOpen when control goes

In a Usercontrol Grid(Grid1) I have a textBox, two Buttons (Search and Save Buttons) and Two Popups(Popup1 and Popup2). Inside Popup2 there is a textBox and one button(Search). I wanted to hide the popups(both 1 and 2) when when the user click outside the Grid1. Rightnow I can hide the Popups but NOT able to click the button inside Popup2. As soon as I Click the SearchButton inside the Popup2 it hides Popups.
I have set the StaysOpen Property for both Popups to a bool prop like this: StaysOpen="{Binding PopupStaysOpen}"
Thanks.
-Menon
private void Grid1_LostFocus(object sender, RoutedEventArgs e)
{
(this.DataContext as ViewModel).PopupStaysOpen = false;
}

Enable/disable a button until an item is selected in a listbox?

How can i disable a button until an item is selected in a listbox?
Initially you disable your button:
button1.Enabled = false;
Then you subscribe to the SelectedIndexChanged event of the listbox. Bellow is the handler:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex >= 0)
{
button1.Enabled = true;
}
else
{
button1.Enabled = false;
}
}
You subscribe to the event from the Visual Studio IDE, or programatically like this:
listBox1.SelectedIndexChanged+=new EventHandler(listBox1_SelectedIndexChanged);
Since you mention winforms, one way is to set the property of the button IsEnabled=false in the property explorer. Then add an event for the listbox OnSelectionChanged. VS will automatically insert the code behind for you, then you can put this in the event handler:
button1.IsEnabled = listbox1.SelectedIndex > 0;
If you are using WPF you can do it the same way, but it would be better to use Databinding.

Resources