Getting item from Observable Collection or List() - c#

I have a ListBox called NotesList. I have an ObservableCollection called noteList, and I have a TextBox called NoteContents.
In my ObservableCollection, I set the Filename and Contents properties for a few items and then it gets added (bound) to my ListBox.
But now, I want to (when I click a button), show the "Contents" of the ListBox Item that was selected in the NoteContents TextBox.
How can I do this?
I currently have:
private void NotesList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
NoteContents.Text = noteList.Where(x => x.Filename.Contains(NotesList.SelectedValue.ToString())).FirstOrDefault().Contents;
}

You can do this without button clicks, just binding like:
<ListBox Name="NotesList" ItemsSource="{Binding YourObservableCollection}">
<!--Your bindings here-->
</ListBox>
<TextBox Text="{Binding ElementName=NotesList, Path=SelectedItem.Contents}" />

Related

How to get the content text from the selected item ListPicker Windows Phone 8.1 Silverlight?

I have a problem here to take the text from selected item inside of my ListPicker,i found this code who allows me to do that
var content = ((ListPickerItem)CursoLista.SelectedItem).Content;
and my XAML:
<toolkit:ListPicker x:Name="CursoLista" Header="Curso" ItemsSource="{Binding}">
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<StackPanel>
<toolkit:ListPickerItem Content="{Binding Curso}"/>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
</toolkit:ListPicker>
as you can see,the content is Binding to a List from my server:
private void Cliente_ProfessorRetrieveCompleted(object sender, Service.ProfessorRetrieveCompletedEventArgs e)
{
CursoLista.ItemsSource = e.Result;
but when i try to do this,i have an Execption :
Additional information: Unable to cast object of type 'FaculdadeGuararapes.Service.ListaProfessor' to type 'Microsoft.Phone.Controls.ListPickerItem'.
FaculdadeGuararapes.Service.ListaProfessor its the list who comes from my WebServer!
First of all, if your e.Result is a list of strings and you set CursoLista.ItemsSource from code behind, you don't need to add ItemsSource in xaml. Next, if you want to retreive single selected item, just pin to SelectionChanged event. Additionally, probably it's not necessary to add <DataTemplate>. DataTemplate is only necessary when you want to customize a layout or bind to the class.
<toolkit:ListPicker x:Name="CursoLista" Header="Curso" SelectionChanged="CursoLista_SelectionChanged">
</toolkit:ListPicker>
and handle it in code behind:
private void CursoLista_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string selectedString = (sender as ListPicker).SelectedItem as string;
}

Grid View Items contents in xaml (windows store app)

how can I get to the contents of the clicked grid Item in a grid view ... for example the grid item has a textbox ... how could I get the contents of this textbox when the gridview Item is clicked vie ItemClickEventArgs ?
thanks
Try this code.
<GridView x:Name="gv" SelectionChanged="gvSelectionChanged">
<GridViewItem>
<TextBox x:Name="txtOne" />
</GridViewItem>
<GridViewItem>
<TextBox x:Name="txtTwo" />
</GridViewItem>
</GridView>
private void gvSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var SelectedText = ((TextBox)((GridViewItem)gv.SelectedItem).Content).Text;
}
Assuming you're using ItemsSource with your GridView, you can use e.ClickedItem to get the DataContext of the GridViewItem you clicked. If you want the actual GridViewItem you can use
GridViewItem item = (sender as GridView).ItemContainerGenerator.ContainerFromItem(e.ClickedItem) as GridViewItem;
Then you can use VisualTreeHelper.GetChild to dig down as necessary (like to find a TextBox).
If you are using a DataTemplate to display the items in the GridView, I have found the simplest way is to capture Tapped, Hold, etc.. event on the DataTemplate's control that holds all the item.
In the event you can code
private void ItemTapped(object sender, TappedRoutedEventArgs e)
{
var clickedItem = (sender as FrameworkElement).DataContext;
}
That will give you the object within the item that was clicked.

Setting Label object as per Listbox selected item

I need to bind a Label to two ListBoxes. In order to do this I have set the SelectionChanged property of both ListBoxes to the same function:
<ListBox Name="ListBox1" SelectionChanged="UpdateSelectedItem" />
<ListBox Name="ListBox2" SelectionChanged="UpdateSelectedItem" />
<Label Name="DetailsLabel" DataContent="DefinedElsewhere" />
However I am having trouble finding what the selected item actually is. I have gone through all the properties of the sending object and the SelectionChangedEventArgs but I cannot find it. The ListBox is bound to an ObservableCollection of objects, and I would like the Label to display the properties of the last selected item, no matter from which ListBox it was selected. How do I find that?
private void UpdateSelectedItem(object sender, SelectionChangedEventArgs e)
{
DetailsLabel.Content = ???;
}
You can read the selected item text doing something like:
ListBoxItem item = ((ListBox)sender).SelectedItem as ListBoxItem;
String itemText = (item != null) ? item.Content.ToString() : String.Empty;
You have to cast the SelectedItem property to the type of object you have in the list.
In this example I've used ListBoxItem.

get data from datagrid on button click in WPF application

I have a datagrid which consists of a checkbox and couple of columns.
When the customer clicks the checkbox I am firing grid selectionchanged event which displays some data from selectedrow to the label.
But I need that selected row data when I click a button as well.
Is there any good way to retrieve that?
Based on your comment you should try this then (the DataGrid is named dataGrid in XAML):
private void Button1_Click(object sender, RoutedEventArgs e)
{
// If the grid is populated via a collection binding the SelectedItem will
// not be a DataGridRow, but an item from the collection. You need to cast
// as necessary. (Of course this can be null if nothing is selected)
var row = (DataGridRow)dataGrid.SelectedItem;
}
Could use the Tag (Edit: If you use a CheckBoxColumn you can use the styles to do this, if you have trouble with that i could give an example):
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="Button1_Click"
Tag="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
private void Button1_Click(object sender, RoutedEventArgs e)
{
var button = (FrameworkElement)sender;
var row = (DataGridRow)button.Tag;
//...
}

listview.selectionchanged, can I make it fire everytime I click an item?

Is there a way to make the selectionchanged event fire every time a selection in the listview is clicked, instead of only when it changes?
For example, lets say i have a listview with only one object in it. The user clicks that object, and that object contains information that populates some textboxes below. The user starts changing some of the values in these textboxes (which are not bound to the object). They then decide that they dont want what is in those text boxes so they'd like to reset everything to what is in the object in the listview. But when they click the one object in the listview, nothing happens, because the selection has not changed.
Hope that makes sense. Anyone know how I can get around this?
The ListView.SelectionChanged and ListViewItem.Selected events are not going to re-fire if the item is already selected. If you need to re-fire it, you could 'deselect' the item when the event fires.
private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
foreach (var item in e.AddedItems.OfType<ListViewItem>())
{
Trace.WriteLine("ListViewItem Selected");
item.IsSelected = false;
}
}
Thus allowing you to re-select it ad nauseum. However, if you don't need the actual selection then you should be using an ItemsControl.
If you do want to maintain the select-ability of the item(s) then you should look at registering to a different event than ListView.SelectionChanged, or ListView.Selected. One that works well for this is PreviewMouseDown, as like the initial item selection we want it to occur on both left and right clicks. We could attach it to the single ListViewItem, but since the list may at some point gain more items, we can assign it to all items by using the ItemContainerStyle property of the ListView.
<ListView SelectionChanged="ListView_SelectionChanged">
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<EventSetter Event="PreviewMouseDown"
Handler="ListViewItem_PreviewMouseDown" />
</Style>
</ListView.ItemContainerStyle>
<ListViewItem>Item 1</ListViewItem>
<ListViewItem>Item 2</ListViewItem>
<ListViewItem>Item 3</ListViewItem>
<ListViewItem>Item 4</ListViewItem>
</ListView>
private void ListViewItem_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
Trace.WriteLine("ListViewItem Clicked: " + (sender as ListViewItem).Content);
}
private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(ListView.SelectedIndex != -1)
{
//to do staff
}
ListView.SelectedIndex = -1;
}
also we can use this one!
<ListView x:Name="ListView"
Height="Auto" SelectionChanged="ListView_OnSelectionChanged"
Width="260"
Margin="0,-12,0,-25">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding name_to_show_menu,Mode=TwoWay}" Tapped="UIElement_OnTapped"></TextBlock>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
and in code behind
private void UIElement_OnTapped(object sender, TappedRoutedEventArgs e)
{
//this fire every time
}

Categories

Resources