Search code examples
c#wpflistviewdata-bindinglistviewitem

How to get a not selected item from wpf's listview


I'm working on a image displayer and I custom a datatemplate of listview(it's the images container) with two overlapping button to show the image is liked or disliked, and I need to do something when I click them, but I found that the item won't be selected if I just click the button, this makes I cannot get the item of the button which i clicked

I've try to google it but there's no help, all answers I got need to get the selecteditem at first, but this is where the question is

this is my data template

<DataTemplate DataType="model:PictureViewModel">
    <Grid x:Name="ImageContentPresenter" Width="150" Height="150">
        <Border x:Name="Border1" CornerRadius="15" Background="#FAFAFC" />
        <Grid Height="150" Width="150">
            <Grid.OpacityMask>
                <VisualBrush Visual="{Binding ElementName=Border1}" />
            </Grid.OpacityMask>
            <Border Background="#FAFAFC" x:Name="ImageCornerBorder"
                    CornerRadius="15,15,15,15" />
            <Image x:Name="DisplayedImage" Stretch="Uniform"
                   Source="{Binding Path=ImageSource,Mode=OneWay,Converter={StaticResource BitmapImageConverter}}"
                   Loaded="DisplayedImage_OnLoaded">
                <Image.OpacityMask>
                    <VisualBrush Visual="{Binding ElementName=ImageCornerBorder}" />
                </Image.OpacityMask>
            </Image>
            <Grid HorizontalAlignment="Right" VerticalAlignment="Top" Width="30"
                  Height="30" Margin="0,10,10,0">
                <Grid.Resources>
                    <util1:VisibilityConverter x:Key="VisibilityConverter" />
                    <mainWindow:ButtonType x:Key="LikeButtonType">Like</mainWindow:ButtonType>
                    <mainWindow:ButtonType x:Key="DisLikeButtonType">DisLike</mainWindow:ButtonType>
                </Grid.Resources>
                <Button x:Name="LikeButton" Style="{DynamicResource MaterialDesignToolButton}" Visibility="{Binding IsLiked,Converter={StaticResource VisibilityConverter},ConverterParameter={StaticResource LikeButtonType}}" Click="LikeButton_OnClick">
                    <Viewbox Width="24" Height="24">
                        <Canvas Width="24" Height="24">
                            <Path
                                Data="M12.1,18.55L12,18.65L11.89,18.55C7.14,14.24 4,11.39 4,8.5C4,6.5 5.5,5 7.5,5C9.04,5 10.54,6 11.07,7.36H12.93C13.46,6 14.96,5 16.5,5C18.5,5 20,6.5 20,8.5C20,11.39 16.86,14.24 12.1,18.55M16.5,3C14.76,3 13.09,3.81 12,5.08C10.91,3.81 9.24,3 7.5,3C4.42,3 2,5.41 2,8.5C2,12.27 5.4,15.36 10.55,20.03L12,21.35L13.45,20.03C18.6,15.36 22,12.27 22,8.5C22,5.41 19.58,3 16.5,3Z"
                                Fill="Gray" />
                        </Canvas>
                    </Viewbox>
                </Button>
                <Button x:Name="DislikeButton" Style="{DynamicResource MaterialDesignToolButton}" Visibility="{Binding IsLiked,Converter={StaticResource VisibilityConverter},ConverterParameter={StaticResource DisLikeButtonType}}">
                    <Viewbox Width="24" Height="24">
                        <Canvas Width="24" Height="24">
                            <Path
                                Data="M12,21.35L10.55,20.03C5.4,15.36 2,12.27 2,8.5C2,5.41 4.42,3 7.5,3C9.24,3 10.91,3.81 12,5.08C13.09,3.81 14.76,3 16.5,3C19.58,3 22,5.41 22,8.5C22,12.27 18.6,15.36 13.45,20.03L12,21.35Z"
                                Fill="Crimson" />
                        </Canvas>
                    </Viewbox>
                </Button>
            </Grid>
        </Grid>
    </Grid>
</DataTemplate>

the LikeButton and DislikeButton are those two buttons, how exactly can I get the item where i clicked button is


Solution

  • You could access PictureViewModel object by accessing the DataContext property on the Button object in your LikeButton_OnClick method.

    The ideal way would be to use Commands. You could have LikeCommand defined on the PictureViewModel itself and access it via this from the Command Handler.