Reputation: 73
XAML:
<ListBox Height="163" HorizontalAlignment="Left" Margin="0,-24,0,0" Name="listBox2" VerticalAlignment="Top" Width="446" SelectedValuePath="" SelectionChanged="listBox2_SelectionChanged" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock x:Name="LblPassword" HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding RegistPassword}" VerticalAlignment="Top" Visibility="Collapsed" Foreground="Black"/>
<TextBlock x:Name="LblSecQn" HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding SecurityQn}" VerticalAlignment="Top" Margin="10,50,0,0" Visibility="Visible" Foreground="Black"/>
<TextBlock x:Name="LblSecAns" HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding SecurityAns}" VerticalAlignment="Top" Visibility="Collapsed" Foreground="Black"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBlock x:Name="LblEmail" Text="" HorizontalAlignment="Left" Margin="24,59,0,0" TextWrapping="Wrap" Foreground="Black" FontSize="30" VerticalAlignment="Top" Visibility="Collapsed"/>
<Button x:Name="BtnSubmit" Content="Submit" HorizontalAlignment="Left" Margin="116,316,0,0" VerticalAlignment="Top" Height="88" Width="193" Click="BtnSubmit_Click" Foreground="Black" BorderBrush="Black"/>
.CS:
private void BtnSubmit_Click(object sender, RoutedEventArgs e)
{
ListBoxItem listBoxItem = listBox2.SelectedItem as ListBoxItem;
DataTemplate listBoxTemplate = listBoxItem.ContentTemplate;
StackPanel outerStackPanel = listBoxTemplate.content as StackPanel;
StackPanel innerStackPanel = outerStackPanel.Children[1] as StackPanel;
TextBlock nameBox = innerStackPanel.Children[2] as TextBlock;
nameBox.Visibility = Visibility.Visible;
}
This is my currently my BtnSubmit_click. However,there is some exception on the listBoxTemplate.content part.
I want to make Visibility of LblSecAns to be visible when i clicked on BtnSubmit. I searched ans on google. It is telling me to use Visual tree helper. But i doesn't understand and doesn't know how to apply.
Upvotes: 0
Views: 793
Reputation: 1386
There are two ways to achieve what you want to do here, if I understood correctly.
The first one is like you mentioned with the Visual Tree Helper. Basically every element with a Name, x:Name can be "accessed" using the command: var nameBox = GetTemplateChild("LblSecAns") as TextBox;
.
I haven't specifically tested this code on your example (retrieve something from ItemTemplate) but it should work.
The other way and the best one from my point of View is to create a converter. I have been using converters for many reasons, including item's visibility so far and it makes things simpler while keeping the code clean.
A visibility converter would be like:
public class VisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (bool)value) ? Visibility.Collapsed : Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
return Visibility.Visible;
}
}
Then your TextBlock
would change to:
<TextBlock x:Name="LblSecAns" HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding SecurityAns}" VerticalAlignment="Top" Visibility="{Binding notAnswered, Converter={StaticResource VisibilityConverter}}" Foreground="Black"/>
And finally your button onClick method will simply be:
private void BtnSubmit_Click(object sender, RoutedEventArgs e)
{
ListBoxItem listBoxItem = listBox2.SelectedItem as <<ClassOfTheItem>>;
listBoxItem.notAnswered = false;
}
Don't forget to add the notAnswered
property to the item's class <<ClassOfTheItem>>
you are binding the listbox to. Since I don't have your code I suggested adding a new property but this step might not be necessary since Visibility
can be binded to something else, i.e. the actual answer.
Upvotes: 1