I want to get the tapped item in my gridview, but I can't use itemclick event with some reason, after get the item, I also want to convert it as an object which the item hold. The gridview had DataTemplate, when I see the e.OriginalSource value in tapped event, it is a TextBlock. Anyone can help?
I answered a similar question here:
Windows 8 Metro: How to get clicked GridViewItem?
If you're trying to get the GridViewItem object for the item the user clicked on, this can be captured by the GridView's ItemClick event by using the GridView's ItemContainerGenerator.ContainerFromItem method.
Step 1: Enable IsTapEnabled="True" and Create Tapped event in Gridview
<GridView x:Name="categoryItemsGV"
Margin="5,5,0,0"
IsItemClickEnabled="True" IsTapEnabled="True" Tapped="categoryItemsGV_Tapped"
SelectionMode="Single"
Step 2: This GridView bind with TableModel list, and below Tapped Event
private void categoryItemsGV_Tapped(object sender, TappedRoutedEventArgs e)
{
var selectedTableModel= (TableModel)(sender as GridView).SelectedItem;
}
This selectedTableModel contain selected items model.
You can use "onItemInvoked" event of control ListView as below code:
//item click handler for list view
document.getElementById("yourListViewId").addEventListener("iteminvoked", function(e) {
e.detail.itemPromise.then(function (invokedItem) {
WinJS.Navigation.navigate("url to another pages", { objectToTransfer: invokedItem.data });
});
});
Using "invokedItem.data" to pass whole object data which ListViewItem holds
You can try using attached behavior aka WinRT Behavior and use it to relay tapped event to a command in your view model
The DataContext of your TextBlock is what you want.
Related
I'm programming a simple listbox in windows phone 7. The listbox has some items and when I click any of items the app navigates to a new page.
From beginning to here, everything is good.
But I want end-user can select the item again which causes navigate to the next page again. But listbox as it is, doesn't allow me to select an already selected item again.
I tried to do this for allowing to select an item again.
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(ListBox.SelectedIndex != -1 )
{
NavigationService.Navigate(uri);
ListBox.SelectedIndex = -1;
}
}
I have alreadhy edited the ListBoxItemTemplate to highlight selected item, but when I use above code, I cannot highlight the selected item, because it changes SelectedIndex too fast.
So how can I allow user to select a selected item or how can I highlight previous selected item. Any suggestions or tips?
EDIT:
When using a normal ListBox , I can simply use :
listboxitem1.Background = new SolidColorBrush(Color.Blue);`
But when I edit template of listbox item this does throw exception, so I cannot still do this.
Look into implementing one of the other Event handlers for Mouse events on the ListBox. Such as the Click Event. You can detect when the user clicks the ListBox without changing the selected index by keeping track of the previous selected index value.
Well, you can edit the template of ListBoxItem and add a grid, fully front of listbox item. And bind the visibility property of this grid.
After this, you can change of the visibility according to selected index. This approach should work well if you're using a navigation with listbox.
Hope it works!
I have editable combobox, MVVM.
I need dropdown=true when I write something in combobox.
Text="{Binding textsearch, UpdateSourceTrigger=PropertyChanged}"
here in textsearch I wrote OnPropertyChange for ComboBox IsDropDownOpen=true, but it works only onсe, when I select row, then try to write again, the dropdown=false and not react to property...
Also, when I selected row and change text, I can't select this row again, because it was selected, I need change selected item first, but when I change selected Item, the text changes too.
How to make filter works?
Or like another variant, I added textbox for filter text, but when I write text and call textsearch property, the textbox lost focus and combobox has this focus... how to save focus on textbox and dropdown combobox itemslist?
The easiest way to do something like that is to handle one or more events. You could try to handle the PreviewTextInput event:
<ComboBox ItemsSource="{Binding Items}" IsEditable="True"
PreviewTextInput="ComboBox_PreviewTextInput" />
...
private void ComboBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
ComboBox comboBox = sender as ComboBox;
comboBox.IsDropDownOpen = true;
}
The code in this event handler will open the drop down each time a user types into the ComboBox TextBox. You may need to handle a few more events to get your exact desired behaviour, but you can see which events are available from the ComboBox Events page on MSDN and complete this yourself.
I have maybe an easy question but I would like to ask about possibilities how to bind textbox Text property to combobox SelectedItem property. I do it through combobox SelectedItemChanged event and set text like this:
if(cmbMeasuring.SelectedItem != null)
txtMethod.Text = ((ListBoxItem)cmbMeasuring.SelectedItem).Value;
I have class ListBoxItem which holds 2 strings "Name" and "Value". Then I created BindingList for combobox:
private BindingList<ListBoxItem> lst;
and then set combobox data source in constructor:
cmbMeasuring.DataSource = lst;
cmbMeasuring.DisplayMember = "Name";
This works fine but I dont know if its the best way how to do it. But problem occurs when I change the textbox content. I do it through textbox Leave event:
private void txtMethod_Leave(object sender, EventArgs e)
{
if (cmbMeasuring.SelectedItem != null)
((ListBoxItem)cmbMeasuring.SelectedItem).Value = txtMethod.Text;
}
If textbox lost focus I assign item value. But I have also a menustrip to save input and when I click to it directly this event dont occur so the last input is not saved. I know that this could be done through textbox TextChanged event but it consume a lot of time.
Do you have any better solutions or is it OK? Im not using WPF.
Thanks.
If you have a Click event for the MenuStrip item, you can do the following
MyMenuStripItem.Focus();
This should cause the MenuStrip item to gain focus and therefore causing the TextBox to lose focus.
Try data binding on the TextBox in your form's constructor:
txtMethod.DataBindings.Add("Text", lst, "Value",
false, DataSourceUpdateMode.OnPropertyChanged);
I need to delete a selected element in a ListView using a delete button. The ListView is being filled using data collections and a web service to bring the data from a database. I'm also using MVVM.
This is the click event for the button:
private void Click_Button_Delete(object sender, RoutedEventArgs e)
{
Collection3VM.DinamicSummary.Remove(Collection3VM.DinamicSummary.---);
}
Where Collection3VM is the resource reference for the ViewModel named in the xaml code, and DinamicSummary is the property created in the ViewModel. What command or should I use where the --- are?
You are not doing it the right MVVM way. You can declare a property in your view model with a type equal to type of the collection items, then in your xaml and inside the ListBox element you can do something like this:
SelectedItem = {Binding Path=TheNameOfThePropertyYouChoseInYourViewModel,
Mode=OneWayToSource}
and then associate the delete button with a Command on the same view model and the button is clicked then you can do something like this in your view model:
var selectedItem = TheNameOfThePropertyYouChoseInYourViewModel;
yourCollection.Remove(selectedItem);
DinamicSummary should be of type ObservableCollection.
I have a listbox in my winform, when an item in the listbox is selected, a value is placed into a textbox in the same Form. There my many items in my listbox which when selected, i want the text box to be empty so i can pass in the new value. How do i check is if the user has clicked on something other their initial selected item? i get the currently selected item like this below:
var selectedItem = (ReportItems)listbox.selectedItem
You can use the SelectedIndexChanged event on your ListBox . You can create an event handler for this event to determine when the selected index in the ListBox has been changed. This can be useful when you need to display information in other controls based on the current selection in the ListBox. You can use the event handler for this event to load the information in the other controls.
See MSDN documentation: link
You can add a global variable for your ReportItems and call it 'selItem'.
After the user changed the selected Item you check the "new" selectedItem with the 'selItem'-variable.. i don't think that a listbox has a method that can check if the selection has changed from the previous one..
I'm not sure if there is a reason you're not leveraging the SelectionChanged event of the ListBox or not but you should be if you're not. Further, determining if it's different than the initially selected item should be pretty straight forward because you can save the initially selected item in a private variable in your form and compare every time the method handler for SelectionChanged fires.
Other than this, there's not much more I can suggest because your questions isn't terribly clear and there is no code to look at.
My solution was to always clear the textbox first. So as soon as a user selects an item in the listview, rather than populating the textbox straight away, clear the textbox before populating it.
clearText(); is called soon as a listbox item is clicked.
public void clearText()
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
textBox6.Clear();
textBox7.Clear();
textBox8.Clear();
textBox9.Clear();
textBox10.Clear();
textBox11.Clear();
textBox12.Clear();
}