C# Windows Universal App ListView Selected Item Content - c#

I'm building a Universal Windows App and using the ListView, upon click I can't seem to get the selected item text/item details in general..
My XAML Code:
<ListView Name="listviews" Margin="10,172,10,0" Tapped="listviews_Tapped" SelectionChanged="listviews_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Bookings.ID}" Visibility="Collapsed" />
<TextBlock>
<Run Text="{Binding Bookings.first}" FontSize="24"></Run>
<Run Text="{Binding Bookings.last}" FontSize="32"></Run>
</TextBlock>
<TextBlock Tag="{Binding Bookings.ID}">
<Run Text="Total: "/>
<Run Text="{Binding Bookings.pax}" />
<Run Text=" - M: "/>
<Run Text="{Binding Bookings.total_male}"></Run>
<Run Text=" / F: "/>
<Run Text="{Binding Bookings.total_female}"></Run>
</TextBlock>
<ToggleSwitch x:Name="toggleSwitch1" OnContent="Checked-in" OffContent="Waiting" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I tried using Tapped & SelectionChanged but can't seem to grab the data... Any suggestions?
Thanks!

If you want to catch it on ItemClick event , you must set ListView's ItemClickEnabled property to True and SelectionMode to none.
After that , in the second parameter of ItemCLick event handler (e parameter usually) you can catch your model with e.ClickedItem property.
Don't forget to cast it to your Model like :
var myClickedItem = e.ClickedItem as Bookings

Please try to add this to your Page.cs file:
void listviews_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Bookings SelectedBook = (Bookings)listviews.SelectedItem;
var abc = SelectedBook.ID;
}
Now you can access all properties of Bookings-typed object using SelectedBook.xyz

Related

WPF Binding TextBlock value to display SelectedItem in ComboBox

I have a ComboBox and a couple of TextBlock fields.
I want to display the properties of a SelectedItem from the ComboBox on those Textblock's. Image
So that when I choose one of multiple user's the properties in the TextBlock will update to those of the SelectedItem. I have found an example, although it is using silverlight, and does not work entirely.
<ComboBox Grid.Row="0"
Grid.Column="0"
VerticalAlignment="Bottom"
VerticalContentAlignment="Center"
HorizontalContentAlignment="Left"
Margin="0"
Height="40"
Name="ComboBox"
ItemsSource="{Binding UserModels}"
SelectedItem="{Binding EnteredUserModel, Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding FirstName}"
Style="{StaticResource ResourceKey=ComboBoxItemTextBlock}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
<TextBlock Grid.Row="1"
Grid.Column="0"
Margin="0 10 0 10" >
<Run Text="{DynamicResource firstName}" />
<Run Text=": " />
<Run Text="{Binding ElementName=ComboBox, Path=SelectedItem, UpdateSourceTrigger=PropertyChanged}" />
</TextBlock>
This is what I've tried. I added Name to the ComboBox so that I can access it's SelectedItem in my TextBlock. I need to get the SelectedItem.firstname, etc. At this stage i can only access the entire objects.
Am I missing some useful binding?
In order to show the FirstName property of the SelectedItem, just use an appropriate property path, i.e. SelectedItem.FirstName:
<Run Text="{Binding ElementName=ComboBox, Path=SelectedItem.FirstName}" />
or, since SelectedItem is bound to an EnteredUserModel property in your view model:
<Run Text="{Binding Path=EnteredUserModel.FirstName}" />
Setting UpdateSourceTrigger=PropertyChanged is not necessary. It has no effect in a OneWay Binding.
You're getting the EnteredUserModel-Object, because that's the selected item of the ComboBox. If you want the displayed text you must bind to the FirstName-Property.
Alternatively you can bind to EnteredUserModel.FirstName in your TextBox

Universal Windows App - I have a ListView of products, Add to Cart button.

Needless to say, I am a beginner at Universal Windows Apps development, and I need help. I have a ListView with items representing products displayed as catalog. Each item includes 'Add to Cart' button. My question is: When the button is tapped, how do I pass the specific item for which the button has been tapped to a method which puts the product in the cart. Here is my xaml page:
<ListView Name="productsList">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>...
....
<StackPanel Grid.Column="1" Margin="10">
<TextBlock Name="productName" FontSize="14" Text="{Binding Name}" ></TextBlock>
<TextBlock Name="productPrice" FontSize="14" Text="{Binding Price}" Margin="0,5" ></TextBlock>
<Button Name="addToCart" Content="Stavi u korpu" FontSize="14" Tapped="addToCart_OnTapped" ></Button>
</StackPanel>...
....
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
When the button 'addToCart' is tapped I would like to pass the item as parameter to the following method in C# code, which puts the product in shopping cart:
private void addToCart_OnTapped(object sender, TappedRoutedEventArgs e)
{
// Code to put the product into a cart. This code is not important right now. What is important is how do I pass it a proper parameter?
}
The easiest way for a beginner to solve this is to pass your object as a Tag.
<StackPanel Grid.Column="1" Margin="10">
<TextBlock Name="productName" FontSize="14" Text="{Binding Name}" ></TextBlock>
<TextBlock Name="productPrice" FontSize="14" Text="{Binding Price}" Margin="0,5" ></TextBlock>
<Button Name="addToCart" Tag="{Binding .}" Content="Stavi u korpu" FontSize="14" Tapped="addToCart_OnTapped" ></Button>
</StackPanel>
Binding . refers to the current object in your list.
So on your event you can get the specific button you clicked on, and get the it m out of the Tag.
var button = (Button)sender;
var obj = (Product)button.Tag;

I am using WPF and I have DataTemplate that is i want to access into the codebehind how I can use this?

I am using WPF and I have DataTemplate that is i want to access into the codebehind how I can use this?
<DataTemplate x:Name="PersonDateTemplate">
<StackPanel Orientation="Horizontal">
<Label x:Name="lblhr" Height="40px" Width="50px"
Content="{Binding Path=hrvalueinitially}" FontSize="20px"
HorizontalAlignment="Left" Background="#555555" Foreground="White"
FlowDirection="LeftToRight"></Label>
<TextBlock x:Name="items" Text="{Binding}" Margin="35,0,0,0"></TextBlock>
</StackPanel>
</DataTemplate>
If you are having the DataTemplate in Resource and you have Key defined, you can access the resource in CodeBehind as follows,
DataTemplate dataTemplate = App.Current.TryFindResource("PersonDateTemplate") as DataTemplate;
or if you want create from scratch in CodeBehind, you should use FrameworkElementFactory
You can use dataTemplate to replace the visual appearance of a data item in a control like ListBox, ComboBox or ListView.
To understand how to work with dataTemplate I've done the following example:
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ID}" FontSize="24"/>
<TextBlock Text=". Name: " FontSize="24"/>
<TextBlock Text="{Binding Name}" FontSize="24"/>
<TextBlock Text=" ,Age: " FontSize="24"/>
<TextBlock Text="{Binding Age}" FontSize="24"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
For better understanding about the data template you may follow the following link:
https://msdn.microsoft.com/en-us/library/ms742521(v=vs.110).aspx

How to acces text of textblock inside a dynamic stackpanel in wp8?

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

How do I change the font on the bound part of a TextBlock's content?

Given the below TextBlock, how do I make the SomeString part of the text bold?
<TextBlock Text="{Binding SomeString,StringFormat='{}Row: {0}'}" />
ie: If SomeString = "ABC" I want the TextBlock to look like this:
Row: ABC
Try something like this
<StackPanel Orientation="Horizontal">
<TextBlock Text="Row:"/>
<TextBlock FontWeight="Bold" Text="{Binding SomeString}"/>
</StackPanel>
Basically, you could format each individual Run inside the same TextBlock.
Through XAML
<TextBlock>
<Run>Row:</Run>
<Run FontWeight="Bold" Text="{Binding SomeString}"></Run>
</TextBlock>
MSDN Section
Hope this helps.

Categories