I have a data binded ListBox containing a stackpanel, which contains a number of textbox and the text is being dynamically loaded from server. This is how the XAML looks like-
<ListBox x:Name="SearchColl_List" ItemsSource="{Binding }" >
<ListBox.ItemTemplate>
<DataTemplate>
<!--Main Stack-->
<StackPanel x:Name="Coll_Stack" Orientation="Vertical"
Margin="10,0,6,20"
Tap="StackPanel_Tap">
<TextBlock x:Name="Name_Text"
Text="{Binding Name}"
FontSize="20"
Width="450"
HorizontalAlignment="Left"
Foreground="#33706b"
TextWrapping="Wrap"
/>
<TextBlock Text="{Binding Stream}"
Width="450"
HorizontalAlignment="Left"
FontSize="20"
Foreground="Green"
TextWrapping="Wrap"
/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I have an event "StackPanel_Tap" and I want to access the Text in "Name_Text"(TextBox) for the stack panel element that is being tapped. I want to access the text in following Method Stub...
private void StackPanel_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
//Access the Text in "Name_Text" for current element that is tapped
}
Try this:
((sender as StackPanel).FindName("Name_Text") as TextBlock).Text
Another way would be:
(sender as StackPanel).Children[0].Text
Related
I am implementing 4 RadioButtons in one ListView row and binding them with the model properties. The problem is when the user changes the pre-setted property and the CheckedChanged event fires, how can I get on which row(Index) the user has changed the property?
Here is my Implementation of ListView.
<ListView x:Name ="personlist" ItemsSource="{Binding PersonList}" ItemSelected="personlist_ItemSelected" SelectionMode="Single">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid RowDefinitions="*" ColumnDefinitions="3*,*,*,*,*" BackgroundColor="{Binding RowColor}">
<Label Text="{Binding Name}" Grid.Row="0" Grid.Column="0" VerticalTextAlignment="Center" FontSize="20" TextColor="Black"/>
<RadioButton Value="Travel" IsChecked="{Binding Travel, Mode=TwoWay}" Grid.Row="0" Grid.Column="1" CheckedChanged="RadioButton_CheckedChanged"/>
<RadioButton Value="Reading" IsChecked="{Binding Reading, Mode=TwoWay}" Grid.Row="0" Grid.Column="2" CheckedChanged="RadioButton_CheckedChanged"/>
<RadioButton Value="Sports" IsChecked="{Binding Sports, Mode=TwoWay}" Grid.Row="0" Grid.Column="3" CheckedChanged="RadioButton_CheckedChanged"/>
<RadioButton Value="Dance" IsChecked="{Binding Dance, Mode=TwoWay}" Grid.Row="0" Grid.Column="4" CheckedChanged="RadioButton_CheckedChanged"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
In .xaml.cs
private void RadioButton_CheckedChanged(object sender, CheckedChangedEventArgs e)
{
// I want to get an index of the list item here.
}
Is there any way to achieve it? this way or other.
Thanks in advance!
use the BindingContext
private void RadioButton_CheckedChanged(object sender, CheckedChangedEventArgs e)
{
var button = (RadioButton)sender;
var item = (MyClassName)button.BindingContext;
}
item will be the object that is bound to that row in the ListView
I have the following XAML but I can't seem to access the contents of the ItemsWrapGrid in the corresponding .CS file - can anyone tell me what I should be doing (here's the code behind and xaml) :
private void wifiTapped(object sender, TappedRoutedEventArgs e)
{
Debug.WriteLine("in here " + e.GetType().ToString());
ItemsWrapGrid wg = (ItemsWrapGrid) sender;
Debug.WriteLine(e.OriginalSource.ToString());
foreach (Control c in wg.Children)
{
Debug.WriteLine("Control " + c.Name);
}
Debug.WriteLine("leaving ");
}
<GridView VerticalAlignment="Top" ItemsSource="{Binding nets}" x:Name="GDView" ItemClick="gdViewClick" >
<GridView.ItemTemplate>
<DataTemplate x:Name="configDataTemplate" x:DataType="data:wifiNets" >
<StackPanel Height="300" Width="350" Margin="10" Name="dtStackPanel" >
<Image Source="Assets\wifiIcon.png" Width="200" Height="201" />
<StackPanel Orientation="Horizontal">
<TextBlock Text="Name" Margin="0,0,10,0"/>
<TextBlock Name="configSSID" Width="auto" Text="{x:Bind NetSSID}" FontSize="24" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Strength" Margin="0,0,10,0"/>
<!--<TextBlock Name="configStrength" Width="auto" Text="{x:Bind NetSSIDStrength}" FontSize="20" />-->
<ProgressBar Name="configProgBar" Maximum="5" Value="{x:Bind NetSSIDStrength}" Foreground="Green" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Connected" Margin="0,0,10,0"/>
<TextBlock Name="configConnectedTo" Text="{x:Bind NetSSIDConnected}" FontSize="20"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid MaximumRowsOrColumns="10" Orientation="Vertical" Tapped="wifiTapped" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
Just to be clear, when I run this I have three items of data (so it's working) - but what I'd like to be able to do is click on any one of the three data items and be able to identify the individual controls and their values within the data panel.
Thanks in advance, this is driving me nuts.
Paul.
Set IsItemClickEnabled to true on your GridView, and hook into the ItemClick event on the GridView itself. From the event args, you can get the sender (most likely the GridViewItem UI element itself, of which your DataTemplate content is a child), and the ClickedItem, which is the bound datacontext of the datatemplate - in your case the instance of the data:wifiNets - which if your bindings work mean you won't actually have to look in the VisualTree at all.
If for some reason you want to recurse through the VisualChildren for the items of any ItemsControl, use the ContainerFromIndex or ContainerFromItem methods on the ItemsControl to get the ItemContainer hosting each instance of the datatemplate - although I wouldn't recommend doing this unless you really need too. Ideally you shouldn't often have a need for manually trawling the visual tree.
I have an ObservableCollection<Tag> Tags where the Tag class contains only a string Content property. I've created a DataTemplate that displays all tags and shows small buttons to delete and add new tags.
<DataTemplate>
<Border BorderThickness="1" BorderBrush="#676B6E" Margin="3">
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding Content, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Tag="{Binding}" Padding="0" Margin="2,0"/>
<Button Style="{StaticResource RibbonButton}" Click="ButtonRemoveTagClick" Tag="{Binding}" Padding="0">
<Image Height="12" Width="12" Source="/My Application;component/Resources/cross.png" />
</Button>
</StackPanel>
</Border>
</DataTemplate>
When I add a new Tag to the collection I want the autocreated textbox to automatically select all the text inside and grab focus.
Is there an appropriate event I can handle on the textbox itself, or is there a better way to handle this?
I tried with WpfExtendedToolkit.AutoSelectTextBox but does not work the way I want.
In this case the Loaded event of the TextBox did the trick.
private void TextBox_Loaded(object sender, RoutedEventArgs e)
{
TextBox tb = sender as TextBox;
tb.Focus();
tb.SelectAll();
}
Thanks to Clemens for the quick response.
I build this code so that i have multiple pins on the map. locations and text based on binding:
<Grid x:Name="LayoutRoot" Background="Transparent">
<maps:Map x:Name="myMap" Loaded="myMap_Loaded">
<toolkit:MapExtensions.Children>
<toolkit:MapItemsControl Name="Items">
<toolkit:MapItemsControl.ItemTemplate>
<DataTemplate>
<toolkit:Pushpin GeoCoordinate="{Binding Coordinate}" Tap="Pushpin_Tap_1" >
<toolkit:Pushpin.Template >
<ControlTemplate >
<Canvas>
<Image Source="/App;component/Assets/pin.png"
Width="48" Height="102"
Canvas.Left="-20" Canvas.Top="-102" />
<Border Background="Black" Width="200" Visibility="Visible" x:Name="border1" HorizontalAlignment="Center" >
<StackPanel x:Name="_Stack1" >
<TextBlock x:Name="TextBlock1"
Text="{Binding ID}"
Canvas.Top="-45"
Canvas.Left="5"
Style="{StaticResource PhoneTextNormalStyle }"
Foreground="#FF51FF00" />
<TextBlock
Text="{Binding Name}"
Canvas.Top="-25"
Canvas.Left="5"
Style="{StaticResource PhoneTextContrastStyle }"
Foreground="Red" />
</StackPanel>
</Border>
</Canvas>
</ControlTemplate>
</toolkit:Pushpin.Template>
</toolkit:Pushpin>
</DataTemplate>
</toolkit:MapItemsControl.ItemTemplate>
</toolkit:MapItemsControl>
</toolkit:MapExtensions.Children>
</maps:Map>
</Grid>
Now my question is how i can make a Tab event for the pushpin based on the TextBlock1.Text (I can`t "reach" the textblock1.text in the .cs part)
private void Pushpin_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
{
NavigationService.Navigate(new Uri("/ExtraPage/Info.xaml?selectedItem=" + TextBlock1.Text(THIS WONT WORK) , UriKind.Relative));
}
What is the best way to do this? Or am I doing it all wrong?
Thanks in advance
You can't access it because it's in the template.
If you want the tap event on TextBlock, just add it there instead of adding it to the pushpin. But I assume that's not really what your need.
If you want the Text value from TextBlock1, what you really want is the ID which is bound to that TextBlock. The sender for that event handler that you have is (probably) a Pushpin. So, what you really want is the DataContext of that Pushpin and the ID property of that DataContext, and from what I can tell, the DataContext is some sort of a location object, so I'll call it Location.
var id = ((sender as Pushpin).DataContext as Location).ID;
And that's what's in your TextBlock - id which you can easily use.
Change 'Location' to whatever your class is.
I have the following XAML code, having removed the styling and formatting tags:
<ListBox Name="ManageImageList">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Name="ManageImageThumbnail" Source="{Binding ImageName}" />
<StackPanel Orientation="Vertical" >
<TextBlock Name="ManageImageUrl" Text="{Binding ImageName}" />
<TextBlock Name="ManageImageComment" Text="{Binding Comment}" />
</StackPanel>
<Button Name="ManageImageDelete" ClickMode="Press" Click="ManageImageDelete_Click" Content="X" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The ListBox is bound to an ObservableCollection. I would like to give focus to the parent ListBox item when the button is clicked, making it the SelectedItem of the ListBox. How do I do this?
In the click event use:-
ManageImageList.SelectedItem = ((Button)sender).DataContext;