I need to change the source of an Image control inside a gridview datatemplate when a click event is raised in uwp. When i click on a car image, this Image needs to be modified and displayed the brand logo. I succeed with that code:
<controls:AdaptiveGridView x:Name="AdaptiveGridViewControl"
animations:ReorderGridAnimation.Duration="350"
Margin="0,12,0,0"
ItemHeight="200"
DesiredWidth="200"
SelectionMode="Single"
IsItemClickEnabled="True"
ItemClick="GridView_ItemClick"
>
<controls:AdaptiveGridView.ItemTemplate>
<DataTemplate x:DataType="data:MyData">
<StackPanel Orientation="Horizontal">
<controls:ImageEx x:Name="ImageExControl"
MaxHeight="200"
IsCacheEnabled="True"
Source="{x:Bind carsPictures}"
Stretch="Fill"
PlaceholderSource="/Assets/placeholder_cover.jpg"
PlaceholderStretch="Uniform"/>
</StackPanel>
</DataTemplate>
</controls:AdaptiveGridView.ItemTemplate>
</controls:AdaptiveGridView>
Thanks to that post : How to access a Control inside the data template in C# Metro UI in the code behind,
i can use FindChildControl(DependencyObject control, string ctrlName) method.
In code behind:
private void GridView_ItemClick(object sender, ItemClickEventArgs e)
{
var newData = (MyData)e.ClickedItem;
ImageEx ex = FindChildControl<ImageEx>(this, "ImageExControl") as ImageEx;
ex.Source = newData.brandLogo;
}
The problem is this gridview contains 30 cars picture and only the first Image control is modified when a click event is raised. I don't know how to use the AdaptiveGridView.SelectedItem to change the clicked Image control.
You need to Get GridViewItem that has been Clicked so that you can change the image of the Clicked Item. Change your ItemClick Event to something like below.
private void GridView_ItemClick(object sender, ItemClickEventArgs e)
{
GridViewItem item = myGridView.ContainerFromItem(e.ClickedItem) as GridViewItem;
MyData newData = (MyData)e.ClickedItem;
ImageEx ex = FindChildControl<ImageEx>(item, "ImageExControl") as ImageEx;
ex.Source = newData.brandLogo;
}
Hope This Helps.
Related
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;
}
i want to do some coding on textblock tap event for which, i required its text value
my xaml is like below
<ListBox x:Name="listBox1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Height="auto" >
<TextBlock Name="myTextBlock" Text="{Binding Busnumber}" Tap="buss_Tap" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
and then on textblock tap event i want to get tapped clicked text value
private void buss_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
//i want to achieve this
// string aa= myTextBlock.text;
//but this is not working so what to do here to achieve the same?
}
Try this:
private void buss_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
TextBlock txt = (TextBlock)sender;
MessageBox.Show(txt.Text);
}
You will get TextBlock in sender parameter. Typecast it to Textblock and can get text from there:
string text = ((TextBlock)sender).Text;
I have a longlistselector like the below image. now I wanna get the text of the item user's tapped. I've searched a lot but no solution found ;(
pay attention to the image please to give a sample code
http://amiryari.persiangig.com/image/stackoverflow-question.jpg
1) Wire up the SelectionChanged event on the LongListSelector control:
<phone:LongListSelector ItemsSource="{Binding MyListItems}"
SelectionChanged="LongListSelector_SelectionChanged">
2) Retrieve the selected item from the AddedItems collection in the SelectionChangedEventArgs:
private void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
{
var item = e.AddedItems[0];
}
}
3) If your item is an object, and the text is displayed through a property, then you would have access to the text through the property on your object:
MyListItemObject item = e.AddedItems[0] as MyListItemObject;
MessageBox.Show(item.FullName);
If your list is bound to a list of strings, then it would simply be the first item in the AddedItems collection:
string fullName = e.AddedItems[0].ToString();
MessageBox.Show(fullName);
You can always listen for the SelectionChanged event and obtain the string. There is another way if you are using a DataTemplate to style your items in the list. Declare Tapped event in DataTemplate like this:
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ContactImage}"/>
<TextBlock x:Name="NameTextBlock" Text="{Binding ContactName}" Tapped="NameTextBlock_Tapped"/>
</StackPanel>
</DataTemplate/>
Now in our code:
private void LongListSelector_SelectionChanged(object sender, BlahBlah e)
{
var tb = sender as Textblock;
string cName = tb.Text; //This is the string you wanted.
MessageBox.Show(cName);
}
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.
I am building a small Windows Phone application which has a databound ListBox as a main control. DataTemplate of that ListBox is a databound ItemsControl element, which shows when a person taps on a ListBox element.
Currently, I am accessing it by traversing the visual tree of the application and referencing it in a list, and than getting the selected item through SelectedIndex property.
Is there a better or more effective way?
This one works currently, but I am afraid if it would stay effective in case of larger lists.
Thanks
Have you tried wiring the SelectionChanged event of the ListBox?
<ListBox ItemsSource="{Binding}" SelectionChanged="ListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<!-- ... -->
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
With this in the code behind:
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox listBox = sender as ListBox;
// nothing selected? ignore
if (listBox.SelectedIndex != -1)
{
// something is selected
}
// unselect the item so if they press it again, it takes the selection
listBox.SelectedIndex = -1;
}
ListBoxItem item = this.lstItems.ItemContainerGenerator.ContainerFromIndex(yourIndex) as ListBoxItem;
Then you can use the VisualTreeHelper class to get the sub items
var containerBorder = VisualTreeHelper.GetChild(item, 0) as Border;
var contentControl = VisualTreeHelper.GetChild(containerBorder, 0);
var contentPresenter = VisualTreeHelper.GetChild(contentControl, 0);
var stackPanel = VisualTreeHelper.GetChild(contentPresenter, 0) as StackPanel; // Here the UIElement root type of your item template, say a stack panel for example.
var lblLineOne = stackPanel.Children[0] as TextBlock; // Child of stack panel
lblLineOne.Text = "Some Text"; // Updating the text.
Another option is to use services of the GestureServices class available in the WP7 Toolkit.
You'll need to add a GestureListner to the Root Element of your DataTemplate like so:
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Controls:GestureService.GestureListener>
<Controls:GestureListener Tap="GestureListener_Tap" />
</Controls:GestureService.GestureListener>
<TextBlock x:Name="lblLineOne" Text="{Binding LineOne}" />
<TextBlock Text="{Binding LineTwo}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
And in the GestureListener_Tap event handler, you use this snippet.
private void GestureListener_Tap(object sender, GestureEventArgs e)
{
var itemTemplateRoot = sender as StackPanel;
var lbl1 = itemTemplateRoot.Children[0] as TextBlock;
MessageBox.Show(lbl1.Text);
}
I'm not sure how the GestureListner recognize internally the item being tapped but I guess that it uses the VisualTreeHelper, at least this method is more concise.