How to select the textbox in data template - windows-phone-7

while programming for windows phone 7, I created a listbox and using DataTemplate which contains a textblock and a textbox. The textbox is hided by default.
The XAML:
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<Canvas Width="460" Height="60" Background="{StaticResource PhoneAccentBrush}">
<TextBlock Text="{Binding data}" FontSize="30" Margin="10,10,10,0"/>
<TextBox Height="60" Width="460" Visibility="Collapsed"/>
</Canvas>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The effect I wanna accomplish is : Tap textblock to hide textblock while show textbox.
CODE BEHIND:
private void TextBlock_Tap(object sender, GestureEventArgs e)
{
TextBlock.Visibilty = Visibility.Collapsed;
TextBox.Visibilty = Visibility.Visible;
}
However, obviously the selector isn't correct. I tried to add Name for textbox and textblock, but Name seems does not work in Data Template. Is there anyone who can tell me how can i select the textblock and textbox in a Data Template please? Many Thanks!!!

Try this :
<ListBox Name="lst" >
<ListBox.ItemTemplate>
<DataTemplate>
<Canvas Width="460" Height="60" Background="{StaticResource PhoneAccentBrush}" Tap="Canvas_Tap">
<TextBlock Text="{Binding}" FontSize="30" Margin="10,10,10,10"/>
<TextBox Height="60" Width="460" Visibility="Collapsed" Text="text"/>
</Canvas>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And in code behind:
private void Canvas_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
var m = (sender as Canvas).Children;
foreach (UIElement x in m)
{
if ((x as TextBlock) != null)
(x as TextBlock).Visibility = Visibility.Collapsed;
if ((x as TextBox) != null)
(x as TextBox).Visibility = Visibility.Visible;
}
}

Setting the Name on the template does work, but you can't access it directly!
Instead, try it like this:
<ListBox x:Name="MyListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<Canvas Width="460" Height="60" Background="{StaticResource PhoneAccentBrush}">
<TextBlock x:Name="MyTextBlock" Text="{Binding data}" FontSize="30" Margin="10,10,10,0" Tap="MyTextBlock_Tap" />
<TextBox x:Name="MyTextBox" Height="60" Width="460" Visibility="Collapsed"/>
</Canvas>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And the code:
private void MyTextBlock_Tap(object sender, GestureEventArgs e)
{
var elem = (FrameworkElement)sender;
var myTextBlock = (TextBlock)elem.FindName("MyTextBlock");
var myTextBox = (TextBox)elem.FindName("MyTextBox");
myTextBlock.Visibility = Visibility.Collapsed;
myTextBox.Visibility = Visibility.Visible;
}

Related

MenuFlyout for Items in GridView in Windows 8.1 HubApp

I have Windows 8.1 Hub App that will eventually be released for tablets. I want to add MenuFlyout to GridView. So if I right click or hold then flyout will display. I've gone through several videos, tutorials etc. but none of them relates to GridView. Only for ListView on WindowsPhone. So none of them show how I should approach.
Is it possible? How I should deal with it?
HubSection
This is how my Hubsection with GridView looks like.
<HubSection x:Name="CalcButtons" x:Uid="calculator" Header="{Binding Res.calc2Header, Source={StaticResource SharedStrings}}">
<DataTemplate>
<GridView
ItemsSource="{Binding}"
AutomationProperties.AutomationId="ItemGridView"
AutomationProperties.Name="Items In Group"
ItemTemplate="{StaticResource StandardCalcButton}"
SelectionMode="None"
IsItemClickEnabled="True" Margin="0,0,0,0" ItemClick="GridView_ItemClick">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
</DataTemplate>
</HubSection>
StandardCalcButton
I've created MenuFlayout as resource in App.xaml and attached it to GridViewItem DataTemplate. So it looks like this.
<MenuFlyout x:Key="CalcFlyout">
<MenuFlyoutItem Text="Test1"/>
<MenuFlyoutItem Text="Test2"/>
</MenuFlyout>
<DataTemplate x:Key="StandardCalcButton" >
<Grid Height="180" Width="180" Margin="5,5,5,5" FlyoutBase.AttachedFlyout="{StaticResource CalcFlyout}" >
<Border Height="180" Background="{StaticResource AppYellow}">
<StackPanel Grid.Row="1" Margin="0,0,0,0">
<Image Source="{Binding ImagePath}" AutomationProperties.Name="{Binding Title}" Height="130" Width="180"/>
<TextBlock Text="{Binding Title}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="NoWrap" TextLineBounds="Tight" TextAlignment="Center"/>
<TextBlock Text="{Binding Subtitle}" Style="{StaticResource BodyTextBlockStyle}" TextWrapping="NoWrap" TextLineBounds="Tight" LineStackingStrategy="BlockLineHeight" TextTrimming="CharacterEllipsis" TextAlignment="Center" />
</StackPanel>
</Border>
</Grid>
</DataTemplate>
This is how I want to make Flyout to open in GridView_ItemClicked
private void GridView_ItemClick(object sender, ItemClickEventArgs e){
FrameworkElement senderElement = e.ClickedItem as FrameworkElement;
if (senderElement == null)
{
Debug.WriteLine("null");
return;
} else
{
FlyoutBase flyoutBase = FlyoutBase.GetAttachedFlyout(senderElement);
flyoutBase.ShowAt(senderElement);
}
}
''ClickedItem'' is object which I deliver in ''GridView.DataContext'' so there is no way that it will be castable to FrameworkElement.
Now I'm stuck. I'll be thankful for any guidance.
You can try the following:
private void GridView_ItemClick(object sender, ItemClickEventArgs e)
{
var gridViewItemsControl = sender as ItemsControl;
var clickedItemContainer = gridViewItemsControl.ContainerFromItem(e.ClickedItem);
Debug.WriteLine(clickedItemContainer);
var clickedGridViewItem = clickedItemContainer as GridViewItem;
var clickedItemGrid = clickedGridViewItem.ContentTemplateRoot as Grid;
FlyoutBase flyoutBase = FlyoutBase.GetAttachedFlyout(clickedItemGrid);
flyoutBase.ShowAt(clickedItemGrid);
}
Hope i helps! :)

How to access listboxitem template data when an event of it's item control is fired?

I want to access each listboxitem template control's(image and 4 textblock controls )data when the button is clicked from the following hierarchy.
<ListBox x:Name="lstBoxNearbyPlaces" ItemsSource="{Binding Items}" Grid.Row="2" Margin="0,10,0,0" >
<ListBox.ItemTemplate>
<DataTemplate>
<Button Width="400" Height="150" Name="btnPlace" Click="btnPlace_Click_1">
<Button.Content>
<StackPanel Orientation="Horizontal" >
<Image Name="ImgPlace" Source="{Binding PlaceIconURL}" Width="50" />
<TextBlock Name="lblPlaceLat" Text="{Binding PlaceLatLocation}" Visibility="Collapsed"/>
<TextBlock Name="lblPlaceLng" Text="{Binding PlaceLongLocation}" Visibility="Collapsed"/>
<StackPanel Width="350" >
<TextBlock FontSize="20" Text="{Binding PlaceName}" Name="txtAddress" TextWrapping="Wrap" ></TextBlock>
<TextBlock FontSize="20" Text="{Binding PlaceVicinity}" Name="txtLocation" TextWrapping="Wrap"></TextBlock>
<Line MinHeight="5"></Line>
</StackPanel>
</StackPanel>
</Button.Content>
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Is it possible to get or read the data of the every control here when the button click event is fired ?
You can get the binding object in the ListBox_SelectionChanged as the selected item.
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ItemModel model= listBox.SelectedItem as ItemModel ;
}
Or In the Buttonclick event you can get it as
private void Button_Click(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
ItemModel model = button.DataContext as ItemModel;
}
Here model is the object which you have bind to that template.

Access control inside a listbox without triggering selectionChanged event

For my listbox in windows phone I added a checkbox for each item with the help of this tutorial (option 2). Before I already had a SelectionChanged event for my listbox. How do I prevent firing the event, when the user just checks the checkbox? The SelectionChanged event should only fire when he hits the textbox in the listbox, but not the checkbox.
Thats my listbox:
<ListBox x:Name="toDoItemsListBox" ItemsSource="{Binding Source={StaticResource cvs}}" Grid.Row="1" Margin="12, 0, 12, 0" Width="440" SelectionChanged="goToNavigation" IsSynchronizedWithCurrentItem="False">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<CheckBox IsChecked="{Binding IsFavorite}" Height="48" Width="48" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" Style="{StaticResource CheckBoxStyle1}"/>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Shortcut}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" />
<TextBlock Text="{Binding BuildingName}" TextWrapping="Wrap" Style="{StaticResource PhoneTextSubtleStyle}" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
These are my check events:
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
ListBoxItem checedItem = this.toDoItemsListBox.ItemContainerGenerator.ContainerFromItem((sender as CheckBox).DataContext) as ListBoxItem;
if (checedItem != null)
{
checedItem.IsSelected = true;
}
}
private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
ListBoxItem checedItem = this.toDoItemsListBox.ItemContainerGenerator.ContainerFromItem((sender as CheckBox).DataContext) as ListBoxItem;
if (checedItem != null)
{
checedItem.IsSelected = false;
}
}
Also I have to mention that the selectionChanged event raises only when I check the checkbox. Not when I uncheck it.
I made it work without the selection changed event. As alternative I used a tap event:
<StackPanel Orientation="Vertical" Tap="StackPanel_Tap">
<TextBlock Text="{Binding Shortcut}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" />
<TextBlock Text="{Binding BuildingName}" TextWrapping="Wrap" Style="{StaticResource PhoneTextSubtleStyle}" />
</StackPanel>
and in Code behind I get my listitem with:
private void StackPanel_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
// Save current POI.
Object object = (sender as StackPanel).DataContext as Object;
...}

WP7 listpicker selectionchanged selectedindex return -1

I have i little and simple problem. I think.
I have to Listpickers where the second depends on the selection from the first.
I've thought that i could easily be done with use of selectionchanged on the first Listpicker and then get the selected index.
<toolkit:ListPicker ExpansionMode="FullScreenOnly" Grid.Row="0" Name="customers" FullModeHeader="Kunder" Margin="10,50,10,10" Width="350" HorizontalAlignment="Left" SelectionChanged="customers_SelectionChanged">
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Title}" Margin="12,0,0,0"></TextBlock>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="16,20,0,20">
<TextBlock Text="{Binding Title}" Margin="16,0,0,0" FontSize="30" FontFamily="{StaticResource PhoneFontFamilyLight}"></TextBlock>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
And the code for selectionchanged:
private void customers_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int selindex = customers.SelectedIndex;
MessageBox.Show("index : " + selindex);
Guid costumerid = customers[selindex].id;
Loadprojects();
}
My problem is that selindex always equals -1 and then i get a out of range exception.
What is the best way to solve this?
private void customers_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int selindex = customers.SelectedIndex;
if (selindex==-1) return;
MessageBox.Show("index : " + selindex);
Guid costumerid = customers[selindex].id;
Loadprojects();
}

How to show list of items when click on button in wp7?

i placed one button in a page .when click on that need to show 1 to 30 numbers in combobox as a popup in that page only.please tell me how to acheive this?
Edit:
I have edited the answer with design,add an image as a local content in the project
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Content="Button" Height="82" HorizontalAlignment="Left" Margin="44,59,0,0" Name="button1" VerticalAlignment="Top" Width="376" Click="button1_Click" />
<ListBox ItemsSource="{Binding item}" Width="376" Name="lst" Margin="56,128,48,76" Background="White">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1" DataContext="{Binding}" BorderBrush="Black">
<StackPanel Width="376" Orientation="Vertical" Height="Auto">
<Image Margin="200,20,-75,5" Height="50" Width="50" Source="{Binding img}"></Image>
<TextBlock Margin="-200,-15,90,3" Height="50" Width="50" Name="text" Text="{Binding text}" Foreground="Black"></TextBlock>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Grid>
lst.visibility = visibility.collapsed;
private void button1_Click(object sender, RoutedEventArgs e)
{
lst.visibility = visibility.visible;
List<Itemss> data = new List<Itemss>();
for (int i = 0; i < 30; i++)
{
Itemss item = new Itemss();
item.text = i.ToString();
item.img = "/images.jpg";
data.Add(item);
}
lst.ItemsSource = data;
}
public class Itemss
{
public string text { get; set; }
public string img { get; set; }
}
}
YOu can make use of the ListPicker for WP7 instead of a ComboBox for WP7.
And to show the ListPicker in a popup, Place the ListPicker in a MessagePrompt.

Resources