Кнопка внутри ListBox ItemTemplate не выбирает элемент wpf mvvm - PullRequest
0 голосов
/ 30 апреля 2020

Привет, у меня та же проблема, что и здесь Кнопка внутри ListBox ItemTemplate не выбирает элемент И я знаю причину, по которой моя кнопка не видит выбранный элемент, это потому, что listbox вызывается, когда 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);
                }));
        }
    } 

1 Ответ

0 голосов
/ 30 апреля 2020
    <ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="IsSelected" Value="True"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</ListBox.ItemContainerStyle>

Я нашел ответ, я просто установил выбранный пункт.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...