访问 ListboxItem 中的控件

Accessing Controls in ListboxItem

我有一个列表框。此 ListBox 具有自定义控件。例如,图像、文本块等。最初文本块是折叠的,图像是可见的。当用户使用鼠标输入并更改该项目中图像的不透明度时,我想让文本块可见。但我无法访问 listboxitems 控件。我给它们命名了,但它们没有显示 up.How 我可以实现吗?我的列表框 XAML 代码:

<ListBox SelectionChanged="MoviesDisplay_OnSelectionChanged" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Background="Black" x:Name="MoviesDisplay">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid  x:Name="MoviesGrid"  Height="355" Width="240" Margin="10,20,20,3" HorizontalAlignment="Left">
                        <Image x:Name="MoviePoster" Opacity="1" Source="{Binding Poster}"></Image>
                        <TextBlock Visibility="Collapsed" x:Name="MovieName" FontSize="18" Text="{Binding Name}" Foreground="White" Margin="0,15,0,321"></TextBlock>
                        <TextBlock Visibility="Collapsed" x:Name="MovieGenre" FontSize="16" Text="" Foreground="White" Margin="0,34,0,302"></TextBlock>
                        <TextBlock Visibility="Collapsed" x:Name="MovieReleaseDate" FontSize="16" Text="{Binding ReleaseDate}" Foreground="White" Margin="0,53,164,284"></TextBlock>
                        <materialDesign:RatingBar Visibility="Collapsed" Foreground="White"
                                                  Value="{Binding Rating}"
                                                  x:Name="MovieRatingBar" Margin="59,314,60,17"
                        />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel IsItemsHost="True" Orientation="Horizontal"  />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>







private async void Window_Loaded(object sender, RoutedEventArgs e)
    {
        client.GetConfig();
        var popularMovies = await client.GetMoviePopularListAsync("en", 1);
        PopularMovies.Clear();
        foreach (var movie in popularMovies.Results)
        {
           // ImgListUrls.Add(client.GetImageUrl("w500",movie.PosterPath).AbsoluteUri);
            Movie mov = new Movie()
            {
                Id = movie.Id,
                Name = movie.OriginalTitle,
                ReleaseDate = movie.ReleaseDate.Value.ToShortDateString(),
                Poster = client.GetImageUrl("w500", movie.PosterPath).AbsoluteUri,
                Rating = movie.VoteAverage
            };

            PopularMovies.Add(mov);

        }
        MoviesDisplay.ItemsSource = PopularMovies; 
       // DownloadImages(ImgListUrls);
    }

您不需要在隐藏代码中访问这些元素。改为使用 DataTriggers 添加样式,例如:

<Image Source="{Binding Poster}">
    <Image.Style>
        <Style TargetType="Image">
            <Setter Property="Opacity" Value="1"/>
            <Style.Triggers>
                <DataTrigger
                    Binding="{Binding IsMouseOver,
                              RelativeSource={RelativeSource AncestorType=ListBoxItem}}"
                    Value="True">
                    <Setter Property="Opacity" Value="0.5"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>

将 TextBlock 放入 StackPanel 并添加类似的样式:

<StackPanel>
    <StackPanel.Style>
        <Style TargetType="StackPanel">
            <Setter Property="Visibility" Value="Collapsed"/>
            <Style.Triggers>
                <DataTrigger
                    Binding="{Binding IsMouseOver,
                              RelativeSource={RelativeSource AncestorType=ListBoxItem}}"
                    Value="True">
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </StackPanel.Style>
    <TextBlock FontSize="18" Text="{Binding Name}" Foreground="White"/>
    <TextBlock FontSize="16" Text="{Binding ReleaseDate}" Foreground="White"/>
</StackPanel>