Windows Phone 8, finding a checkbox item inside listbox - windows-phone-8

I have a checkbox inside a stackpanel inside a listbox. I need to set the visibility of that checkbox based on some logical operations, so I need to find that element in listbox.
I am using the following code to find the checkbox on my PageLoad, after the list`s dataSource has been set.
But on this listboxItem i get a null value and a exception saying : reference is not a valid visual dependencyobject
private void searchListCheckBoxVisibility()
//private void SearchList_Loaded(object sender, RoutedEventArgs e)
{
try
{
ListBoxItem listItem = this.SearchList.ItemContainerGenerator.ContainerFromIndex(2) as ListBoxItem;
CheckBox targetCheckBox = FindFirstElementInVisualTree<CheckBox>(listItem);
}
}
The following is my xaml :
Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox Margin="13,10,7,10" x:Name="SearchList" DoubleTap="SearchList_DoubleTap">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="searchListPanel" Orientation="Horizontal" Width="400" ScrollViewer.HorizontalScrollBarVisibility="Auto">
<StackPanel Orientation="Vertical" Width="340">
<StackPanel Orientation="Vertical">
<TextBlock Text="Name : " FontWeight="Bold" Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
<TextBlock Text="{Binding User}" TextWrapping="Wrap" Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Project : " FontWeight="Bold" Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
<TextBlock Text="{Binding Project}" TextWrapping="Wrap" Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Status : " FontWeight="Bold" Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
<TextBlock Text="{Binding On_OffBoarded}" TextWrapping="Wrap" Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Date : " FontWeight="Bold" Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
<TextBlock Text="{Binding DT_On_OffBoarded}" TextWrapping="Wrap" Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
</StackPanel>
<StackPanel>
<TextBlock Text=""/>
</StackPanel>
</StackPanel>
<CheckBox Width="60"
x:Name="user_Checkbox"
Content="" Tag="{Binding ID}"
Visibility="Collapsed"
Checked="user_Checkbox_Checked"
Unchecked="user_Checkbox_UnChecked"
Style="{StaticResource ResourceKey=MSFTGuidelinesCheckBox}"
IsChecked="False"
/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
I am stuck at this issue for a while, and it has been getting onto my nerves now, Please help.
Thanks in advance.

You could simply use the VisualTreeHelper to find a control within your Listbox data template.
Reference: How to access a specific item in a Listbox with DataTemplate?

Related

Show default message when listbox is empty (Windows Phone 8.1)

I want to display the default message when list box is empty and for same I used following code but this code is not working :
<HubSection x:Name="PivotFromAccount" Style="{StaticResource HubSectionStyle2}" >
<DataTemplate>
<Border Style="{StaticResource brdhubsectionstyle}">
<Grid Grid.Row="1" Style="{StaticResource GridMainStyleHubSession}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Border Style="{StaticResource brdheaderstyle}">
<TextBlock Text="From Account" Style="{StaticResource lblheaderstyle}" />
</Border>
<ListBox x:Name="lstAccountDetailsfrom" Grid.Row="1" ItemsSource="{Binding }"
Style="{StaticResource LstBoxStyleBranches}"
Tapped="lstAccountDetailsfrom_Tapped"
ItemContainerStyle="{StaticResource lstAccountliststyle}"
ItemTemplate="{StaticResource lstAccountlistDataTemplate}" >
</ListBox>
<TextBlock Margin="4" FontStyle="Italic" FontSize="12" Text="List is empty" x:Name="txtmessage" Grid.Row="1" Foreground="Black" />
<i:Interaction.Behaviors>
<ic:DataTriggerBehavior Binding="{Binding ElementName=lstAccountDetailsfrom, Path=Items.Count,Converter={StaticResource DataTriggerBehavior},Mode=TwoWay}" Value="0" ComparisonCondition="GreaterThan">
<ic:ChangePropertyAction TargetObject="{Binding ElementName=txtmessage}" PropertyName="Visibility" Value="Collapsed"/>
</ic:DataTriggerBehavior>
</i:Interaction.Behaviors>
</Grid>
</Border>
</DataTemplate>
</HubSection>
I tried with the Ivalue converter also like below but it is also not much more helpful. It is always sending 0 count to ValueConverter
<TextBlock Margin="4" FontStyle="Italic" FontSize="12" Text="List is empty" x:Name="txtmessage" Grid.Row="1" Foreground="Black" Visibility="{Binding ElementName=lstAccountDetailsfrom,Converter={StaticResource DataTriggerBehavior},Mode=TwoWay}" />
If your data source is an ObservableCollection you can bind to its Count property with a custom IValueConverter than converts 0 to Visible and 1 to Collapsed and use it on the TextBlock. No behavior needed.

Listpicker text-alignment to the right

I have the follwing listpicker
<toolkit:ListPicker ItemsSource="{Binding title}" Name="titlePicker" ExpansionMode="ExpansionAllowed" Grid.Row="1" Margin="0,-50,50,0" Background="White" Foreground="#FFDA3434" Canvas.ZIndex="10" HorizontalAlignment="Right" FontSize="20" Grid.RowSpan="2"/>
that contains 3 items and i would like them to align right, but for some reason i can't find the option to align the text, what am i missing?
You should do this in the ListPickerItemTemplate. Add Template to the page resources (or app resources)
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Name="ListPickerItemTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock HorizontalAlignment="Right" Text="{Binding ItemName}"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Name="ListPickerFullModeItemTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock HorizontalAlignment="Right" Text="{Binding ItemName}"/>
</StackPanel>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
and assign template to the picker:
<toolkit:ListPicker
ItemTemplate="{StaticResource ListPickerItemTemplate}"
FullModeItemTemplate="{StaticResource ListPickerFullModeItemTemplate}"
...

Windows Phone 8 Listpicker ItemTemplate not being used

I have a Listpicker control in a page that I bind some items against and I'm trying to change the item template.
My template look as follows:
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Name="modulePickerFullItemTemplate">
<StackPanel>
<TextBlock Text="{Binding modes}"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Name="modulePickerItemTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="LALA" Foreground="#333"/>
</StackPanel>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
The Listpicker:
<toolkit:ListPicker x:Name="ddlMode" Grid.Row="6" ExpansionMode="FullScreenOnly"
FullModeItemTemplate="{Binding modulePickerFullItemTemplate}"
ItemTemplate="{Binding modulePickerItemTemplate}"
FullModeHeader="Select mode">
</toolkit:ListPicker>
What I see in the item is the value that I bind against the Listpicker, not the LALA value that I would expect.
Anything I'm doing wrong?
You've used
ItemTemplate="{Binding modulePickerItemTemplate}"
in your first code snippet, but it must be
ItemTemplate="{StaticResource modulePickerItemTemplate}"
You should use "Binding" to access data binded to the control and "StaticResource" to access everything you've defined in the Resources like the DataTemplate.
This is what I ended up with, to fix the problem:
<toolkit:ListPicker x:Name="lst"
ExpansionMode="FullScreenOnly"
FullModeHeader="Select">
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<Image Source="{Binding Image}" Stretch="None"></Image>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>

Windows Phone 8: LongListMultiSelector not scrollable

<StackPanel x:Name="LayoutRoot" Background="Transparent" >
<TextBlock Margin="20,20,0,0" Text="Type Text Here" HorizontalAlignment="Left"/>
<TextBox x:Name="SearchTextBox" IsReadOnly="False" HorizontalAlignment="Left" Margin="20,5,0,0" Height="70" Width="400" dp:TextBoxOnTextChangedDependency.UpdateSourceOnChange="True" Text="{Binding SearchBoxText, Mode=TwoWay}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="TextChanged">
<cmd:EventToCommand PassEventArgsToCommand="True"
Command="{Binding ElementName=SearchTextBox, Path=DataContext.SearchTextBox_TextChangedCommand}"
/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
<Grid x:Name="LayoutList" Background="{StaticResource PhoneChromeBrush}">
<toolkit:LongListMultiSelector x:Name="treksLocationItems" Background="Transparent"
ItemsSource="{Binding Path=TreksLocationItems}">
<toolkit:LongListMultiSelector.ItemTemplate>
<DataTemplate>
<Grid Margin="0,5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Width="110" Height="150" Source="{Binding PictureFilename}" VerticalAlignment="Top"/>
<StackPanel Grid.Column="1" VerticalAlignment="Top">
<TextBlock Text="{Binding Name}" Style="{StaticResource PhoneTextLargeStyle}" FontFamily="{StaticResource PhoneFontFamilySemiBold}" Margin="12,-12,12,6"/>
<TextBlock Text="{Binding ShortDescription}" Style="{StaticResource PhoneTextNormalStyle}" TextWrapping="Wrap" FontFamily="{StaticResource PhoneFontFamilySemiBold}"/>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Address:" Style="{StaticResource PhoneTextSmallStyle}"/>
<TextBlock Text="{Binding Path=StreetName}" Style="{StaticResource PhoneTextSmallStyle}" FontFamily="{StaticResource PhoneFontFamilySemiBold}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Site:" Style="{StaticResource PhoneTextSmallStyle}"/>
<TextBlock Text="{Binding Path=Website}" Style="{StaticResource PhoneTextSmallStyle}" FontFamily="{StaticResource PhoneFontFamilySemiBold}"/>
</StackPanel>
</StackPanel>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tap">
<cmd:EventToCommand Command="{Binding Path=DataContext.TapCommand, ElementName=searchItems}" CommandParameter="{Binding Path=Id}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Grid>
</DataTemplate>
</toolkit:LongListMultiSelector.ItemTemplate>
</toolkit:LongListMultiSelector>
</Grid>
</StackPanel>
I have this code in XAML, a textbox for search and a multi line select list. The problem is that the multiLineList is not scrollable even if there are many items. If I remove the stackpanel and the textblock and textbox, the list works ok with scroll enabled. Any suggestion?
Try adding a ScrollViewer above your grid.
<ScrollViewer>
<Grid x:Name="LayoutList" ....>
...
</Grid>
</ScrollViewer>
Or you could probably replace your Grid with a ScrollViewer if you're not using it to position your elements.
Wraping LLMS in a ScrollViewer will destroy the item virtualization and throw an OutOfMemoryException if the collection is too long.
Expose the LLMS' inner LongListSelector, then use the ScrollTo() method to scroll to the item u want.
LongListMultiSelector llms = LongListMultiSelector as LongListMultiSelector;
if (llms != null && llms.ItemsSource.Count > 0)
{
llms.InnerLongListSelector.ScrollTo(llms.ItemsSource[llms.ItemsSource.Count - 1]);
}
I've encountered this weird issue and found an easy solution.
It happens for both Longlistselector and LonglistMultiselector.
When you have several controls in one page, you need to set the height of selector's row to "*" instead of "Auto" in order to make the scrolling function to work correctly.
For example :
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/> //Row def for another row
<RowDefinition Height="*"/> //Row def for your selector
</Grid.RowDefinitions>
If the RowDefinition Height not set to "*" but "Auto", scrolling for Longlistselector will not response to user's action.

WinRT GridView Stacking

In the default application there is a gridview that displays groups of data stacked on top of each other (2 boxes high for each group).
When I implement my grid view it doesn't stack. It is just 1 horizontal list.
I've compared my GridView to the default application and it appears to be VERY SIMILAR. Is there a command I'm missing or do I have to setup my data differently?
Any help is appreciated.
My GridView:
<GridView
x:Name="itemGridView"
AutomationProperties.AutomationId="vehicleGridView"
AutomationProperties.Name="vehicles"
TabIndex="1"
Grid.Row="1"
Margin="83,0,-83,-4"
Padding="116,0,116,46"
ItemsSource="{Binding vehicles}"
ItemTemplate="{StaticResource Standard250x250ItemTemplate}"
SelectionMode="None"
IsItemClickEnabled="True"
ItemClick ="vehicleClick">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Grid Margin="1,0,0,6">
<Button
AutomationProperties.Name="VehicleMake"
Content="{Binding VehicleMake}"
Style="{StaticResource TextPrimaryButtonStyle}"/>
</Grid>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,80,0"/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</GridView.GroupStyle>
</GridView>
Default Example:
<GridView
x:Name="itemGridView"
AutomationProperties.AutomationId="ItemGridView"
AutomationProperties.Name="Grouped Items"
Grid.RowSpan="2"
Padding="116,137,40,46"
ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"
ItemTemplate="{StaticResource Standard250x250ItemTemplate}"
SelectionMode="None"
IsSwipeEnabled="false"
IsItemClickEnabled="True"
ItemClick="ItemView_ItemClick">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Grid Margin="1,0,0,6">
<Button
AutomationProperties.Name="Group Title"
Click="Header_Click"
Style="{StaticResource TextPrimaryButtonStyle}" >
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Title}" Margin="3,-7,10,10" Style="{StaticResource GroupHeaderTextStyle}" />
<TextBlock Text="{StaticResource ChevronGlyph}" FontFamily="Segoe UI Symbol" Margin="0,-7,0,10" Style="{StaticResource GroupHeaderTextStyle}"/>
</StackPanel>
</Button>
</Grid>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,80,0"/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</GridView.GroupStyle>
</GridView>
You need to use WrapGrid in your ItemsPanelTemplate. Try this.
<ItemsPanelTemplate><WrapGrid /></ItemsPanelTemplate>