列表框ItemTemplate中的按钮不能选择项目wpf mvvm

问题描述 投票:0回答:1

嗨,我有同样的问题,在这里 列表框ItemTemplate中的按钮没有选择项目。我知道为什么我的按钮看不到选中的项目,这是因为当MouseDownand按钮被调用时,listbox被调用。但我不知道如何解决这个问题。

<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);
                }));
        }
    } 
wpf events button listbox event-routing
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>

我找到了答案,我只是设置了选定的项目,我知道为什么我的按钮没有看到选定的项目,这是因为listbox在MouseDown和...

© www.soinside.com 2019 - 2024. All rights reserved.