Search code examples
wpfeventsbuttonlistboxevent-routing

Button within ListBox ItemTemplate not selecting item wpf mvvm


Hi i have the same problem as here Button within ListBox ItemTemplate not selecting item And i know the reason why my button doesnt see selected item, it because listbox is called when MouseDown and button is called on Click. But i dont know how to fix it 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);
                }));
        }
    } 

Solution

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

    I find answer i just set selected item.