ListView displays certain images but won't display others - c#

I'm trying to get certain images to display on the side of the scrolling item list created by ListView.
Right now I'm using the data template from http://msdn.microsoft.com/en-us/library/windows/apps/hh780650.aspx
<Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}" Width="110" Height="110">
<Image Source="{Binding Image}" Stretch="Uniform"/>
With Item being a property that is set as follows
if(Type.ToLower().CompareTo("success") == 0)
_image = "/Assets/Ok_16.png";
else
_image = "/Assets/Error_16.png";
So basically if a process was successful, it displays one image, and another if it fails. Right now only the error image will display. I put both of them in the Assets folder and checked the filenames so I don't understand why it won't display the other image. I tried the generic images like Logo.png and they work fine, but when I created a copy of Logo.png and called it Logo1.png, it wouldn't display it.
Any ideas on what could be happening?

Converting my comment into an answer:
right click project -> add existing item -> select your files -> click OK -> put them in the correct folder -> set build action to Resource

Related

How to print listbox contents using C# WPF MVVM

I am using C# WPF (trying to use MVVM) with GalaSoft.MvvmLight. In my View I have a list box
<ListBox x:Name="OutputListBoxWindow"
Grid.Row="5"
Grid.Column="0"
Grid.ColumnSpan="8"
Margin="5,5,5,5"
Height="Auto"
Background="Black"
Foreground="LimeGreen"
ItemsSource="{Binding OutputListBox}"
SelectedIndex="{Binding OutputListBoxIndex}"
IsEnabled="{Binding Enable, Mode=OneWay}"
AutomationProperties.IsColumnHeader="False">
I also have a print button
<Button
Grid.Row="0" Grid.Column="6"
Command="{Binding PrintCommand, Mode=OneWay}"
CommandParameter="{Binding ElementName=OutputListBoxWindow, Mode=OneWay}"
Content="Print"
Margin="20,15,20,15"/>
I have put what I believe to be the Visual (the item I want to print) in as a CommandParameter.
In my ViewModel I have a RelayCommand not ICommand
public RelayCommand<Visual> PrintCommand
{
get
{
return new RelayCommand<Visual>(vdata =>
{
PrintDialog myPrintDialog = new PrintDialog();
if (Convert.ToBoolean(myPrintDialog.ShowDialog()))
{
myPrintDialog.PrintVisual(vdata, "listbox contents Printing.");
}
});
}
}
I put a breakpoint in at the line with PrintVisual and I can see that I have hold of the ListBox as it knows how many lines of data I have. If I continue I get the print dialog. I then choose print to PDF and give it a name. It does create a file but what I get appears to be the main window minus the ListBox. I think at the bottom of the PDF I get just a few pixels of the ListBox as shown below.
[![PDF printout][1]][1]
Thinking it was a ListBox issue I tried to print a TextBlock but now I get a blank. Not sure if it helps but in the Print dialog I choose to have A4 paper etc. The print I get is not A4 and seems to be the size of something else. I was expecting an image inside the A4. What have I missed here please.
Printing PDF from Notepad works so the PDF print driver is working
Update: This is what I think is happening. If I put a name in the the window tag and then print that I get the whole window in full colour. If I put a name in a control and try and print that I get the issue. Looking at it further I saw that I know I have hold of the control and I know its size so I know what size I will print. Here is the issue instead of picking up the correct corner to start to print from it picks up the incorrect corner and start to measure from that. This is why in my image above the top is blank and the bottom has just the start of my list box. I tried this with many different controls and they all print but from an entirely different part of the window. At this stage I have no idea how to fix this.
[1]: https://i.stack.imgur.com/b8GmQ.png

Add icon to button with Visual Studio

I have a simple question. I want to add an icon to a C# WPF Button control. I do not want to have to write C# code, or edit XAML to do this.
However, when I click on the button in the Designer, there is no option under properties to set an image. How do you do this through the Visual Studio GUI?
The easiest/best way to do this is to add an Image control as the Content of the Button.
The property window is somewhat limited in what it can do, and only supports text for that property. This does include bindings, so you could use an Image StaticResource. I couldn't find an easy way to create one from the property designer either though.
So basically, you are stuck with editing XAML. Either with a direct Content property or by creating an element in Resources Its not that bad! Just write:
<Button>
<Button.Content>
<Image ImageSource="..."/>
</Button.Content>
</Button>
Now of course, you could create a custom button that exposed that property via the designer, but thats even more XAML. Its WPF, you are going to have to write XAML, so learning how should be a priority.
Visual Studio 2015:
create a button.
create an image.
set the source of the image to the right file of your resources (e.g. a .png file that you included to your project)
drag the image over the button.
A text shows that asks you to press ALT to replace the text of the button with the image.
I currently don't know how to get both, image and text, for a button.
in XAML, it looks like this:
<Button x:Name="button12" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75">
<Image x:Name="image" HorizontalAlignment="Left" Height="24" VerticalAlignment="Top" Width="24" Source="gfx/new.png"/>
</Button>
you could install FontAwesome on your Computer
Goto http://fontawesome.io and download. Open the downloaded ZIP file and inside the font folder there should be .otf file, Install that!
and in the content of the button you could simply type the desired icons code!
For eg:
if you want your button to look like this
then install FontAwesome Font
and in your button tag type Content="" FontFamily="FontAwesome"
More codes can be found here http://fontawesome.io/cheatsheet/

Image is not displaying in WPF Application?

I'm trying to display an image in wpf application but the image is not displaying in my application, someone give solution for this..
<Image Source="Images/LoadTestInfo.png" Stretch="Fill" HorizontalAlignment="center" Width="1024" Height="700" VerticalAlignment="center" />
Thanks in Advance...
Regards
R.Karthik
Set Build Action as Content and select Copy if newer.
Alright, So you need to right click your solution, and ensure that you have added your image. Do this by right clicking then -> add -> new item. Once you have done this, just change the source of you image and it should display the image. Also set build action to resource.

C# xaml image not showing up in application

When I try to display some smaller (72x72) images in my xaml file as a 20x20 image, they work fine, but when I try to use one that's 96x96, they just don't show up. Here is the code:
<Image Source="/Images.Toolbox;component/Images/defaultIcon.jpg" Height="20" Width="20" Stretch="None"/>
This is how it looks like with the 72x72, how I want it to look.
And this is how it looks when I try to use my 96x96 image.
The working one is a .ico, .png also works. The nonworking one is a .jpg, but this shouldn't matter should it?
Visual Studio > Solution Explorer
right-click on the image file > Properties
make sure the Build Action is set to Resource.

Image not displaying at runtime C# WPF

I have been trying to learn C# but I'm coming across a lot of problems. I am trying to display an image in WPF but for some reason, the image won't show! It appears on the Visual Studio editor but when I run the application it doesn't appear.
Here is some of my code:
This is how I'm trying to display the image:
<Image HorizontalAlignment="Left" Height="100" Margin="273,147,0,0"
VerticalAlignment="Top" Width="100" Source="image.jpg"/>
I have also tried using this:
Source="pack://application:,,,/image.jpg"
Thanks for your help!
In your project:
Create a folder say "img", right click on it and select add existing item add image to folder
Go to properties of the added image, set Build Action as Resource.
It worked for me.
In XAML
<Image HorizontalAlignment="Left" Name="MyImg" Height="80" Margin="273,147,0,0"
VerticalAlignment="Top" Width="100" Source="/img/Desert.jpg"/>
If none of these work, try changing the Build Action to Content.
That's what worked for me after struggling for a long time with this.
Go to the properties for the image in Visual Studio and change "Build Action" to "Resource" and "Copy to Output Directory" to "Copy if newer".
I had to do a rebuild, but then it worked. Cred to swapnil.
please drag the image to a image source,it will be like this /img/image.jpg"/
<Image HorizontalAlignment="Left" Height="100" Margin="273,147,0,0"
VerticalAlignment="Top" Width="100" Source="/img/image.jpg"/>
If none of the above works, try Rebuilding your application from the build menu. Not "Build", but "Rebuild"
For example, this is your project structure
+ProjectName
+--+imagesFolder
| +--amogus.png
|
+--App.xaml
+--MainWindow.xaml
+--etc.
and you want to access the to amogus.png in your xaml window,
You have two ways:
note this way the imagesFolder will be visible in the release build to users
to set amogus.png Build Action to Content and
Copy to Output Directory to Copy always more info,
then rebuild from the build menu, then add this to the window xaml
<Image Source="pack://siteoforigin:,,/imagesFolder/amogus.png" ></Image>
note this way the imagesFolder will be not visible in the release build to users
to set amogus.png Build Action to Resource and
Copy to Output Directory to Do not copy or blank more info,
then rebuild from the build menu, then add this to the window xaml
<Image Source="/imagesFolder/amogus.png" ></Image>
more detail
Right click images on the Solution Explorer, choose Properties and then set the Build Action as Resource.
Did not have to do a clean and rebuild. I tried every combination listed above (I am in VS2017)
Go to Project->Properties->Resources
Select File (drop down with choices of strings, images, icons...)
Click Add Resource->Existing File
Navigate to the image and import it
VS identifies it as an image (mine is PNG) and switches the view to show Image resources
Select the thumbnail of the image and in the Properties of the Image (type should be Bitmap) set Persistence to Embedded in resx
I saved and closed Project Properties as I got confused here before
Go to the Resources folder under the project and select the image (it should be listed for you)
Select the image and set the BuildAction to Embedded Resource
I set the File Action to Copy if Newer
From here I move back and forth between Debug and runtime, various combinations of clean, build and publish and the image has FINALLY been displayed every time.
Last tidbit, the XAML in the dialog looks like this:
<Image Source="pack://siteoforigin:,,,/Resources/DeathSpiral.png" />
I have updated several projects that were supposed to display graphics but didn't always do so using the steps above. They all work now. Tested in both VS2017 and VS2019 and no errors so far.

Categories