Get current selected item in listview - c#

How can I display the currently selected item and its price in a message box?
<ListView Name="listMenuItems" SelectionMode="Multiple" Background="Transparent" SelectionChanged="listMenuItems_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<Border Width="700" Height="80" CornerRadius="5" BorderBrush="Gray" BorderThickness="2">
<Border.BitmapEffect>
<DropShadowBitmapEffect Color="Red" Direction="200" Noise=".6" ShadowDepth="10" Opacity=".6"/>
</Border.BitmapEffect>
<TextBlock Name="txtItem" Height="100" Width="650" FontSize="50" Text="{Binding Path=item}" FontFamily="Georgia"></TextBlock>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

var tempitem=((YourCustomClass)e.ClickedItem).item;
You need to typecast inside your listview Itemclick event then you can access the selected item and price(wasnt able to find its binding in code so assuming).

Related

Access PlaceHolderText property of Textbox and set it to initially value once the Page/User Control loads

I am working on a UWP app and I have implemented a UserControl.The user control has multiple Textboxes and the usercontrol has been implemented in a different view page. I want to make sure that whenever the View page loads, all the textboxes have Enteras the default text that goes off when the textbox is highlighted.
My UserControl has this implementation
<UserControl >
---------------------------
----------------------------
<Border Grid.Row="1" Grid.Column="2" BorderBrush="{StaticResource brush}" BorderThickness="0,3,0,0" >
<local:NumericTextBox x:Name="TaxifuelRevised" IsEnabled="{Binding FuelPlanInfo.IsTappable}" Tag="TaxiFuelActual" TextAlignment="Right" Text="{Binding FuelPlanInfo.TaxiFuelActualInPreferredUnit,ElementName=rootFuelControl,Mode=TwoWay,UpdateSourceTrigger=Explicit}"
Margin="0,0,40,0" Style="{StaticResource TransparentTextBox}" HorizontalAlignment="Right" VerticalAlignment="Center" Foreground="#FF0078D7"
BorderBrush="{x:Null}" Background="{x:Null}" FontSize="40" FontFamily="Helvetica" InputScope="Number"
KeyDown="TextBox_KeyDown" MaxLength="5" GotFocus="TextBox_GotFocus" LostFocus="TextBox_LostFocus" Tapped="KeyBoardInputScope_Tapped" />
</Border>
</UserControl>
The view where it has been implemented is
<Pivot x:Name="MyPivot" Grid.Row="1" ManipulationMode="None" Margin="115,10,90,20" SelectionChanged="MyPivot_SelectionChanged" HorizontalAlignment="Stretch" Width="2536" >
<PivotItem Tag="Fuel Plan" Margin="0" ManipulationMode="None">
<PivotItem.Header >
<StackPanel >
<Image Name="FuelPlanImage" Source="ms-appx:///Assets/fuel_plan_icon.png" Width="82" Height="80"></Image>
<TextBlock Margin="0,8,0,0" TextAlignment="Center" Text="Fuel Plan" FontSize="40"/>
</StackPanel>
</PivotItem.Header>
<local:FuelPlan x:Name="FuelPlanUC" HorizontalAlignment="Stretch" FuelPlanInfo="{x:Bind ViewModel.FuelPlanInfo, Mode=OneWay}"></local:FuelPlan>
</PivotItem>
Checkout the PlaceholderText property of the TextBoxControl.

Trouble with multiple selection of a list box in wp8

I'm using ListBox to list a group of items and i want the user to select multiple items from the list. I tried it with the following code,
<ListBox x:Name="List" SelectionMode="Multiple" HorizontalAlignment="Left" Height="559" Margin="14,1,-1,0" VerticalAlignment="Top" Width="443" SelectionChanged="List_SelectionChanged_1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Width="420" Height="60">
<TextBlock x:Name="tbName" Width="318" Foreground="{Binding color}" Height="35" FontSize="22" Text="{Binding Name}" Margin="-350,13,69,12" />
<Canvas x:Name="ContentPanelCanvas" Grid.Row="1" Background="Transparent" Margin="0,0,12,0">
<Line X1="0" Y1="00" X2="420" Y2="0" Stroke="Gray" StrokeThickness="0.3"/>
</Canvas>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
But still i didn't get a checkbox to select multiple items.
What is the problem?
I think it will be better if you use LongListMultiSelector from Windows Phone Toolkit
You can find code samples here. Also take a look of this question.

Changing border color when a control is selected with c# on windows 8 app store

I have a gridview control with images and text with this xaml:
<Grid Grid.Column="1" Margin="0,40,30,0" >
<GridView x:Name="celebGridView" Margin="0,0,0,0" Padding="0,0,0,0">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal" MaximumRowsOrColumns="3"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.Header>
<StackPanel Width="480" Margin="0,4,14,0">
<StackPanel Orientation="Horizontal" Margin="0,0,0,10">
<TextBlock Text="Most Viewed Celebs" Foreground="black" FontSize="25"/>
<Image Source="/images/Navigation-Right.png" Margin="10,0,0,0"/>
</StackPanel>
</StackPanel>
</GridView.Header>
<GridView.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Source="{Binding ImageBitmap}" />
<TextBlock HorizontalAlignment="Center" Text="{Binding Name_}" Foreground="Black"/>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</Grid>
and i get this purple border around the selected items in the gridview below
How can i override this behaviour, i need to change the color to a custom color (#fdeb01).
You could edit the ItemContainerStyle for the GridView. The easiest way is to use Blend and find the SelectedBorder element and change stroke brush to color you want.
Aside from using Expression Blend, note that Metro ListView selection color modification in XAML is also valid for GridViewItems so changing the following:
<SolidColorBrush x:Key="ListViewItemSelectedBackgroundThemeBrush" Color="#fdeb01"> </SolidColorBrush>
<SolidColorBrush x:Key="ListViewItemPointerOverBackgroundThemeBrush" Color="#ffffff"></SolidColorBrush>
<SolidColorBrush x:Key="ListViewItemSelectedPointerOverBackgroundThemeBrush" Color="#fdeb01"></SolidColorBrush>
<SolidColorBrush x:Key="ListViewItemSelectedPointerOverBorderThemeBrush" Color="#fdeb01"></SolidColorBrush>
will also affect the gridview.

Different Object-Types in Grid- or ListView

I'm currently trying to figure out how to show different types of objects in a GridView, look at this Pic for example:
the last element on the right side is different than the other elements, so if i bind an observablecollection to the GridView, how can i say that the last element is shown up in anohter layout.
currently I'm using this XAML-Code
<GridView x:Name="startView" ItemsSource="{Binding}" Grid.Column="1" Grid.Row="2" SelectionMode="None" Width="Auto">
<GridView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock x:Name="DetailTitle" Height="74" Text="{Binding Title}" />
<Image x:Name="Image" Height="Auto" Width="Auto" Margin="0" Stretch="None" Source="{Binding LocalCoverUrl}" Visibility="Collapsed" />
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal" MaximumRowsOrColumns="2" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
and this Code in the Back:
ObservableCollection<Movie> recentlyStarted = await Api.RecentlyStarted(3);
startView.DataContext = recentlyStarted;
but I have currently no clue how to let the last element show up in a different style
The easy way would be to have the two types of object as different classes (e.g. MoviePicStyle + MoviePlainStyle. Then move your DataTemplate out of the GridView, so that each object is picked up by type,
e.g.
<Window.Resources>
<DataTemplate DataType="{x:Type ViewModel:MoviePicStyle}">
<StackPanel>
<TextBlock x:Name="DetailTitle" Height="74" Text="{Binding Title}" />
<Image x:Name="Image" Height="Auto" Width="Auto" Margin="0" Stretch="None" Source="{Binding LocalCoverUrl}" Visibility="Collapsed" />
</StackPanel>
<DataTemplate DataType="{x:Type ViewModel:MoviePlainStyle}">
...Different View...
</DataTemplate>
</Window.Resources>
<GridView...
Use template selector property of gridview and depending upon the type of object select the template. I did the same in my project. you need to write your own DataTemplateSelector.
I referred below link
http://babaandthepigman.wordpress.com/2012/02/08/datatemplateselector-winrt/

Setting a WPF Image in XAML to a property

I have ListBox in my WPF project that gets set to a datasource of "MyObjectCollection".
I have managed to get the ListBox to display my collection, and each item to display two string properties from the object. The object also contains an Image, how do i get the image to display in the ListBox?
I am currently using the below code to bind to my DataSource
<UserControl.Resources>
<DataTemplate x:Key="CustomerTemplate">
<Border BorderThickness="2" BorderBrush="silver" CornerRadius="5" Padding="1"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid>
<Image Source="{Binding Artwork}" Tag="{Binding Artwork}" VerticalAlignment="Stretch" ></Image>
<TextBlock Text="{Binding Name}" Foreground="#515151"
FontSize="16" HorizontalAlignment="Stretch"
FontWeight="Bold" />
<TextBlock Text="{Binding Length}" Foreground="#515151" Margin="0,25,0,0"
FontSize="10" HorizontalAlignment="Stretch"
FontWeight="Bold" />
</Grid>
</Border>
</DataTemplate>
</UserControl.Resources>
Thanks,
Ben
It depends on the image type in your collection.
If it is a path string to a file or if it is a byte array.
You should use ValueConverter for your image binding.
Take a look at ValueConverter

Categories