i have a list box view in windows phone that i want to delete an item from it upon a specified event .... how can i accomplish that
this is my XAML code
<ListBox x:Name="lstData2"
ItemsSource="{Binding DataCollection2, Source={StaticResource PinsCollection}}" Grid.ColumnSpan="3" Foreground="#FF1D53D0" Height="583" VerticalAlignment="Bottom">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Tap="StackPanel_Tap">
<Image Margin="8"
VerticalAlignment="Top"
Source="{Binding ImageUri}"
Width="100"
Height="100" />
<StackPanel Height="93" Width="259" >
<TextBlock Margin="8"
Width="250"
TextWrapping="Wrap"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Foreground="#FF1D53D0"
Text="{Binding Pinnedname}" Height="33" RenderTransformOrigin="0.5,0.5" FontFamily="Segoe WP SemiLight" FontSize="24" FontWeight="Bold" />
<TextBlock Width="155"
Margin="8,0,8,8"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Text="{Binding Status}" Foreground="#FF1D53D0" FontFamily="Segoe WP SemiLight" />
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu>
<toolkit:MenuItem Header="Remove Pin" Click="RemovePin_Click" Tag="{Binding pinId}"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
You are using binding to show your data (DataCollection2) in a list view. Your list view is updated when your data is modified. To delete an item in the ListView, you should remove the corresponding item in the DataCollection2 and the list view will be automatically updated. Make sure that the DataCollection2 is an ObservableCollection instance.
Related
I'm trying to put any selected item's name in my list box in the textbox next to it. But I've got trouble doing so.
Here's a little bit of my code:
<DockPanel Margin="10">
<StackPanel Margin="5" DockPanel.Dock="Left">
<ListBox Name="lbNames" DisplayMemberPath="Name" IsSynchronizedWithCurrentItem="True"/>
</StackPanel>
<StackPanel Margin="5" DockPanel.Dock="Right">
<TextBox Name="txtName" Width="auto" Text="{Binding ElementName=lbNames,Path=SelectedItem}"/>
<WrapPanel Margin="0,5" Orientation="Horizontal">
<Button Margin="2,0" Name="btnAdd" Click="btnAdd_Click" Content="Add"/>
<Button Margin="2,0" Name="btnEdit" Click="btnChange_Click" Content="Edit"/>
<Button Margin="2,0" Name="btnDelete" Click="btnDelete_Click" Content="Delete"/>
</WrapPanel>
</StackPanel>
</DockPanel>
Because you are showing Text="{Binding ElementName=lbNames,Path=SelectedItem}"and your selected item is an user object.
Instead of this you can use something like that.
First add SelectedValuePath="Name" into your listbox.
Then use Text="{Binding ElementName=lbNames,Path=SelectedValue}"
<DockPanel Margin="10">
<StackPanel Margin="5" DockPanel.Dock="Left">
<ListBox Name="lbNames" DisplayMemberPath="Name" SelectedValuePath="Name" IsSynchronizedWithCurrentItem="True"/>
</StackPanel>
<StackPanel Margin="5" DockPanel.Dock="Right">
<TextBox Name="txtName" Width="auto" Text="{Binding ElementName=lbNames,Path=SelectedValue}"/>
<WrapPanel Margin="0,5" Orientation="Horizontal">
<Button Margin="2,0" Name="btnAdd" Click="btnAdd_Click" Content="Add"/>
<Button Margin="2,0" Name="btnEdit" Click="btnChange_Click" Content="Edit"/>
<Button Margin="2,0" Name="btnDelete" Click="btnDelete_Click" Content="Delete"/>
</WrapPanel>
</StackPanel>
</DockPanel>
Or simply you can use it too Text="{Binding ElementName=lbNames,Path=SelectedItem.Name}"
I'm trying to highlight the selected item in a bridview from a windows application.
To be more precise:
<GridView
x:Name="itemGridView"
AutomationProperties.AutomationId="ItemsGridView"
AutomationProperties.Name="Items"
TabIndex="1"
Grid.RowSpan="2"
Padding="116,136,116,46"
SelectionMode="None"
IsSwipeEnabled="false"
IsItemClickEnabled="True"
ItemClick="openRessource"
ItemsSource="{Binding Source={StaticResource itemsViewSource}}">
<GridView.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Left" Width="200" Height="250" Holding="openHoldMenu">
<Border Background="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}">
<Image Source="{Binding icon}" Stretch="None"/>
</Border>
<Image Source="{Binding downloaded}" Width="30" Height="30" Margin="5" HorizontalAlignment="Right" VerticalAlignment="Top"/>
<StackPanel VerticalAlignment="Bottom" Background="{ThemeResource ListViewItemOverlayBackgroundThemeBrush}">
<TextBlock Text="{Binding Mode=OneWay}" Foreground="{ThemeResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource BaseTextBlockStyle}" Height="60" Margin="15,0,15,0" FontWeight="SemiBold"/>
<TextBlock Text="{Binding description, Mode=OneWay}" Foreground="{ThemeResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource BaseTextBlockStyle}" TextWrapping="NoWrap" Margin="15,0,15,10" FontSize="12"/>
</StackPanel>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
This is my XAML, fairly simple, I just have a Holding event which brings up a menu in my page.
My problem is that I want to know which item has been held to create a border around it so that the user knows which item is selected.
I can easily access the bound item in my list but can't get the grid element selected.
If anyone know how to, that would be really appreciated.
Thank you!
Should be able to make a trigger such as this:
http://stackoverflow.com/questions/4539909/wpf-datagrid-selected-row-style
I'm creating a list of images in a listbox, but i need the images to be clickable.
What I'm having trouble with is getting the value from the child element(textblock) when you click the image.
I have searched all over the net, but i cannot find the solution.
`
<RowDefinition />
</Grid.RowDefinitions>
<StackPanel Grid.Column="0" Grid.Row="0" Margin="10" Grid.ColumnSpan="2">
<TextBlock FontFamily="Tunga" FontSize="30" Foreground="White" Text="WOW" />
<TextBlock FontFamily="Tunga" FontSize="25" Foreground="White" Text="Work Out Warz" />
</StackPanel>
<StackPanel Grid.Column="4" Grid.Row="0">
<TextBlock FontFamily="Tunga" FontSize="36" Foreground="White" Text="" Name="txtname" Height="45"/>
<TextBlock FontFamily="Tunga" FontSize="25" Foreground="White" Text="" Name="txtwowid" />
</StackPanel>
<Grid Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="5" >
<ListBox x:Name="LstImages" Background="Gray" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.PanningMode="HorizontalOnly" Cursor="Hand" SelectionChanged="LstImages_SelectionChanged" IsSynchronizedWithCurrentItem="True" >
<ListBox.ItemTemplate>
<DataTemplate>
<ListBoxItem>
<StackPanel Orientation="Vertical" Margin="10" >
<Button Click="Button_Click" >
<Image Source="{Binding img}" Height="250"/>
</Button>
<TextBlock Name="txtblname" Text="{Binding Title}" Foreground="White" FontSize="30" FontFamily="Tunga"/>
</StackPanel>
</ListBoxItem>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Grid>
`
In order to get the selected item you can bind SelectedImage in your dataContext object which should have Title and img properties to SelectedItem property of ListBox
<ListBox SelectedItem="{Binding Path=SelectedImage, Mode=TwoWay}" x:Name="LstImages" Background="Gray" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.PanningMode="HorizontalOnly" Cursor="Hand" SelectionChanged="LstImages_SelectionChanged" IsSynchronizedWithCurrentItem="True" >
I'm trying to get the index of a ListBoxItem in WP8.... And this is my XAML code
<phone:PivotItem Header="Pins">
<!-- Content Panel -->
<Grid x:Name="ContentPanel2" HorizontalAlignment="Left" Height="583" Margin="10,10,0,0" Grid.Row="1" VerticalAlignment="Top" Width="460">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="400*"/>
<ColumnDefinition Width="0*"/>
<ColumnDefinition Width="87*"/>
</Grid.ColumnDefinitions>
<ListBox x:Name="lstData2"
ItemsSource="{Binding DataCollection2, Source={StaticResource PinsCollection}}" Grid.ColumnSpan="3" Foreground="#FF1D53D0" Height="583" VerticalAlignment="Bottom">
<ListBox.ItemTemplate >
<DataTemplate >
<StackPanel Orientation="Horizontal" Tap="StackPanel_Tap" MouseLeftButtonDown ="SetIndex" Tag="{Binding pinId}">
<Image Margin="8"
VerticalAlignment="Top"
Source="{Binding ImageUri}"
Width="100"
Height="100" />
<StackPanel Height="93" Width="259" >
<TextBlock Margin="8"
Width="250"
TextWrapping="Wrap"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Foreground="#FF1D53D0"
Text="{Binding Pinnedname}" Height="33" RenderTransformOrigin="0.5,0.5" FontFamily="Segoe WP SemiLight" FontSize="24" FontWeight="Bold" />
<TextBlock Width="155"
Margin="8,0,8,8"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Text="{Binding Status}" Foreground="#FF1D53D0" FontFamily="Segoe WP SemiLight" />
</StackPanel>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu>
<toolkit:MenuItem Header="Remove Pin" Click="RemovePin_Click"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<!-- End of Content Panel -->
</Grid>
</phone:PivotItem>
I want to get the index in the SetIndex event handler in the MouseLeftButtonDown!
You should use a two-way binding for that:
<ListBox x:Name="lstData2" SelectedIndex="{Binding Index,Mode=TwoWay}"
There's generally no need to add click handlers for ListBox, as most basic click functionality is built into the control.
I have a twitter feed in a list box in a app I'm making for Windows Phone 7. The problem I'm having is that the text of a tweet is being cut off the edge of the list box instead of wrapping around to a new line like this:
The list box is inside a panorama which works fine. This is my code:
<ListBox x:Name="cheapyListBox" Height="500" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Width="400" ScrollViewer.VerticalScrollBarVisibility="Hidden" HorizontalContentAlignment="Left" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="132" Tap="Message_OnTap">
<Image Source="{Binding ImageSource}" Height="73" Width="73" VerticalAlignment="Top" Margin="0,10,8,0"/>
<StackPanel Width="Auto">
<!--<TextBlock Text="{Binding UserName}" FontSize="28" Margin="12,0,0,0" /> -->
<TextBlock Text="{Binding Message}" TextWrapping="Wrap" FontSize="24" />
<TextBlock Text="{Binding Date}" TextWrapping="Wrap" FontSize="20" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
How can I get the tweet text to wrap round instead of being cut off? Thank you.
Since your inner StackPanel is nested within a horizontal Stackpanel, it has no constraint of depth, and so the TextBlocks expand infinitely as the text becomes longer.
There are a variety of ways you can fix the issue, but an easy one (if you know the width that you want) is to set the inner StackPanel's width to a finite number.
For example:
<ListBox x:Name="cheapyListBox" Height="500" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Width="400" ScrollViewer.VerticalScrollBarVisibility="Hidden" HorizontalContentAlignment="Left" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="132" Tap="Message_OnTap">
<Image Source="{Binding ImageSource}" Height="73" Width="73" VerticalAlignment="Top" Margin="0,10,8,0"/>
<StackPanel Width="400">
<!--<TextBlock Text="{Binding UserName}" FontSize="28" Margin="12,0,0,0" /> -->
<TextBlock Text="{Binding Message}" TextWrapping="Wrap" FontSize="24" />
<TextBlock Text="{Binding Date}" TextWrapping="Wrap" FontSize="20" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Hope this helps!