0

嗨,我有与此处相同的问题ListBox ItemTemplate 中的按钮未选择项目 而且我知道我的按钮看不到所选项目的原因,这是因为当 MouseDown 和单击时调用按钮时调用了列表框。但我不知道如何修复它 XAML

<ListBox  Grid.Row="1" x:Name="ListViewProduct" Background="#FFf1f1f1" ItemsSource="{Binding Videos}" >

        <ListBox.ItemTemplate>
            <!-- FIX Ripple-->
            <DataTemplate >
                <Border  Background="White" Name="border" Margin="50 10 50 10" Width="310" Height="360">
                    <StackPanel>
                        <Border  Width="300" Height="300" CornerRadius="5" Margin="5">
                            <Border.Effect>
                                <DropShadowEffect ShadowDepth="5"/>
                            </Border.Effect>
                            <Border.Background>
                                <ImageBrush ImageSource="{Binding Image}"/>
                            </Border.Background>
                        </Border>
                        <Grid>
                            <StackPanel>
                                <TextBlock Margin="5" Text="{Binding Name}" FontSize="14" FontFamily="Franklin Gothic Medium" />
                                <TextBlock Margin="5 0" Text="{Binding User.Name}" FontSize="13" />
                            </StackPanel>
                            <Button  HorizontalAlignment="Right" Margin="0 0 10 0"
                                    Command="{Binding ElementName= ListViewProduct,Path=DataContext.DownloadPageCommand}" 
                                    CommandParameter="{Binding Path=SelectedItem , ElementName=ListViewProduct}">
                                <materialDesign:PackIcon Width="25" Height="25" Kind="Download"  Foreground="White"/>
                            </Button>
                        </Grid>
                    </StackPanel>
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseLeftButtonUp">
                            <i:InvokeCommandAction Command="{Binding ElementName= ListViewProduct,Path=DataContext.ViewWatchingPageCommand}" 
                                                   CommandParameter="{Binding Path=SelectedItem , ElementName=ListViewProduct}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>

C#

  private RelayCommandParametr _downloadpageCommand;
    public RelayCommandParametr DownloadPageCommand
    {
        get
        {
            return _downloadpageCommand
                ?? (_downloadpageCommand = new RelayCommandParametr(
                obj =>
                {
                    SelectedVideo = obj as Video;
                    _navigationService.NavigateTo("Download",obj);
                }));
        }
    }
    private RelayCommandParametr _viewWatchingPageCommand;
    public RelayCommandParametr ViewWatchingPageCommand
    {
        get
        {
            return _viewWatchingPageCommand
                ?? (_viewWatchingPageCommand = new RelayCommandParametr(
                (obj) =>
                {
                    SelectedVideo = obj as Video;
                    _navigationService.NavigateTo("VideoWatching",SelectedVideo);
                }));
        }
    } 
4

1 回答 1

0
    <ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="IsSelected" Value="True"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</ListBox.ItemContainerStyle>

我找到了我刚刚设置选定项目的答案。

于 2020-04-30T13:44:46.540 回答