-
Sep 2nd, 2020, 08:00 AM
#1
Thread Starter
Junior Member
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
-
Sep 2nd, 2020, 08:27 AM
#2
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?
-
Sep 2nd, 2020, 08:38 AM
#3
Thread Starter
Junior Member
Re: Value 'Image' cannot be converted to type 'Control'
Originally Posted by jmcilhinney
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
-
Sep 2nd, 2020, 08:44 AM
#4
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.
-
Sep 13th, 2020, 11:19 PM
#5
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|