Results 1 to 5 of 5

Thread: Value 'Image' cannot be converted to type 'Control'

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2018
    Posts
    22

    Value 'Image' cannot be converted to type 'Control'

    I'm trying to iterate through 90 Images on a WPF window to set their visibility to hidden and I am getting the error
    Code:
    Value 'Image' cannot be converted to type 'Control'
    The code I've tried is
    Code:
    Dim pctImage As Control = (CType(gridPctSelNum.FindName("pctSelNum" & i), Image))
    If I was using labels instead of images then the above code would work with the "Image" part being changed to "Label"

    Is there an alternative control I should be using instead of Image? I know on WindForms you would use PictureBox, but that isn't and available in WPF

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: Value 'Image' cannot be converted to type 'Control'

    Firstly, you should be using DirectCast rather than CType.

    As for the issue, if the object returned by FindName is an Image then that is the type you should be casting as. An Image is not a Control though. Read the documentation for the types you're using and learn about them. Label inherits Control, which inherits FrameworkElement. Image inherits FrameworkElement directly. Why are you using Control at all? Why isn't the variable type Image if it's supposed to refer to an Image object?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2018
    Posts
    22

    Re: Value 'Image' cannot be converted to type 'Control'

    Quote Originally Posted by jmcilhinney View Post
    Firstly, you should be using DirectCast rather than CType.

    As for the issue, if the object returned by FindName is an Image then that is the type you should be casting as. An Image is not a Control though. Read the documentation for the types you're using and learn about them. Label inherits Control, which inherits FrameworkElement. Image inherits FrameworkElement directly. Why are you using Control at all? Why isn't the variable type Image if it's supposed to refer to an Image object?
    I'm trying to get to grips with WPF and keep using WinForms techniques. I didn't realise that I needed to use Image instead of control.

    I've changed my code from
    Code:
    For i = 1 to 90
     Dim pctImage as Control = CType(gridPctSelNum.FindName("pctSelNum" & i), Image)
     pctImage.Visibility = Visibility.Hidden
    Next
    Which gave me the error.

    To
    Code:
    For i = 1 to 90
     Dim pctImage as Image= DirectCast(gridPctSelNum.FindName("pctSelNum" & i), Image)
     pctImage.Visibility = Visibility.Hidden
    Next
    And it works a charm! Thanks for yorur help, jmcilhinney

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: Value 'Image' cannot be converted to type 'Control'

    I think that it gets a little more confusing when so many of the names are the same but referring to significantly different things but it's important to make as few assumptions as possible about how WPF will work based on WinForms experience. Assume everything is different and consider it a bonus when they're the same.

  5. #5

    Re: Value 'Image' cannot be converted to type 'Control'

    Using this code you can set visibility to hidden.

    Code:
    <DataTemplate x:Key="_HistoryDetails">
          <Grid x:Name="SingleUpload" Background="Transparent" VerticalAlignment="Stretch" HorizontalAlignment="Left" Margin="0,2,0,2">
            <Grid.RowDefinitions>
              <RowDefinition Height="auto"/>
            </Grid.RowDefinitions>
            <StackPanel Orientation="Horizontal" Grid.Row="0">
              <ProgressBar x:Name ="UploadProgress"  Grid.Row="0" Width="70" Height="10" HorizontalAlignment="Left"
                                 VerticalAlignment="Center" Minimum="0" Maximum="10" Value="{Binding ProgressedValue}" Background="White"
                           Visibility="Visible"></ProgressBar>
              <Image x:Name="StatusImage" Width="15" Height="15" Source="{Binding Imgpath}" Visibility="Hidden"></Image>
            </StackPanel>
          </Grid>
          <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding IsLoaded}" Value="True">
              <Setter Property="Visibility" TargetName="UploadProgress" Value="Hidden"/>
              <Setter Property="Visibility" TargetName="StatusImage" Value="Visible"/>
            </DataTrigger>
          </DataTemplate.Triggers>
        </DataTemplate>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width