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;
}
这是我目前的BtnSubmit_click。但是,listBoxTemplate.content部分有一些例外。
当我点击BtnSubmit时,我想让LblSecAns的可见性可见。我在谷歌搜索ans。它告诉我使用Visual Tree helper。但我不了解,也不知道如何申请。
答案 0 :(得分:1)
如果我理解正确,有两种方法可以实现您想要做的事情。
第一个就像你在Visual Tree Helper中提到的那样。基本上每个具有Name,x:Name的元素都可以被&#34;访问&#34;使用命令:var nameBox = GetTemplateChild("LblSecAns") as TextBox;
。
我还没有在您的示例中专门测试此代码(从ItemTemplate中检索某些内容),但它应该有效。
从我的观点来看,另一种方式和最好的方法是创建一个转换器。我一直在使用转换器有很多原因,包括目前为止的项目可见性,它使事情更简单,同时保持代码清洁。
可见性转换器就像:
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;
}
}
然后您的TextBlock
将更改为:
<TextBlock x:Name="LblSecAns" HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding SecurityAns}" VerticalAlignment="Top" Visibility="{Binding notAnswered, Converter={StaticResource VisibilityConverter}}" Foreground="Black"/>
最后你的onClick方法按钮就是:
private void BtnSubmit_Click(object sender, RoutedEventArgs e)
{
ListBoxItem listBoxItem = listBox2.SelectedItem as <<ClassOfTheItem>>;
listBoxItem.notAnswered = false;
}
请勿忘记将notAnswered
属性添加到您要将列表框绑定到的项目<<ClassOfTheItem>>
中。由于我没有您的代码,我建议添加新属性,但这一步可能没有必要,因为Visibility
可以绑定到其他内容,即实际答案。