locked
Access Image inside a View Content RRS feed

  • Question

  • User31568 posted

    Hi,

    I have a ListView

    Inside a ListView I have an Image and I have no problem accessing it using:

    Image FavoriteImage = (Image)sender;
    FavoriteImage.Source = "Favorite.png";
    

    but the problem comes when I want to access this button and try to change the Image source inside it.

    <SyncfusionButtons:SfButton x:Name="ButtonHomePageLike" Grid.Column="1" HeightRequest="25" WidthRequest="25" BackgroundColor="White" BorderWidth="0" Clicked="ButtonHomePageLike_Clicked">
        <SyncfusionButtons:SfButton.Content>
            <Grid HorizontalOptions="Center" VerticalOptions="Center">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
    
                <Image Grid.Row="0" x:Name="ImageButtonHomePageLike" Source="LikeDefault.png" WidthRequest="25" HeightRequest="25" Aspect="AspectFit" HorizontalOptions="Center" VerticalOptions="Center">
                    <Image.Style>
                        <Style TargetType="{x:Type Image}">
                            <Style.Triggers>
                                <DataTrigger TargetType="Image" Binding="{Binding ad_liked}" Value="1">
                                    <Setter Property="Source" Value="Liked.png"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Image.Style>
                </Image>
            </Grid>
        </SyncfusionButtons:SfButton.Content>
    </SyncfusionButtons:SfButton>
    

    I tried this:

    SfButton senderButton = (SfButton)sender;
    Grid gridEditor = senderButton.Children[0] as Grid;
    Image imagebutton = gridEditor.Children[0] as Image;
    imagebutton.Source = "Favorite.png";
    

    but I am getting:

    System.NullReferenceException: 'Object reference not set to an instance of an object.'

    Kindly help..

    Thanks, Jassim

    Sunday, September 29, 2019 12:54 PM

All replies

  • User89714 posted

    @JassimRahma said: I tried this:

    SfButton senderButton = (SfButton)sender;
    Grid gridEditor = senderButton.Children[0] as Grid;
    Image imagebutton = gridEditor.Children[0] as Image;
    imagebutton.Source = "Favorite.png";
    

    but I am getting:

    System.NullReferenceException: 'Object reference not set to an instance of an object.'

    Which line of code is the exception happening on? (you can step through it in VS to find out)

    Post the entire method that that code snippet appears in Post the entire XAML for the ListView

    Sunday, September 29, 2019 2:33 PM