How to set up the image source path properly in XAML - wpf

i'm trying to put a close button image on my newly created tabs for my app, but i just can't seem to get the right path. This is how i've tried inputting it (code is from MainWindow.xaml):
<TabControl Margin="10,26,10,10" ItemsSource="{Binding FileTabs}">
<TabControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FileTabName}" />
<Button Name="closeTabBtn">
<Image Width="20" Height="20" Source="/Images/button-close.jpg" />
</Button>
</StackPanel>
</DataTemplate>
</TabControl.ItemTemplate>
This is my project structure:
What is the correct path for the image source property since i'm kind of new at this ?

You should set the Build Action of the image file to Resource in Visual Studio and try with a pack URI if your relative path still doesn't work:
Source="pack://application:,,,/Images/button-close.png"

Related

Why does my Bitmap Image show in the designer but not in the .EXE

I have a ressource dictionary in a control library to state my bitmap image. key= Refresh -M
UriSource="/Resources/Icons/Basic/refresh-100.png"
You can see in the code below that its instanciated in a button viewbox.
Since my styles dictionary are appart form the main project i made "WPFStyle_Ascendecies.xaml" that include all of them so I only have to include this one in app.xaml
/------Images-----\
View Designer
App.exe
/------#See last button-----\
<!--Left Top to DownMenu -->
<StackPanel Background="DimGray" Grid.Row="0" Grid.RowSpan="3" Grid.Column="0" Grid.ColumnSpan="2">
<TextBlock Text="{Binding Title}" Padding="0,14,0,0" Height="56" Margin="0,0,0,0" FontSize="18" Background="Black" Foreground="White" TextAlignment="Center" />
<Button x:Name="Homepage" Content="Home Page" Height="30" Margin="0,0,0,1" Click="Homepage_Click" />
<Button x:Name="Clients" Content="Clients" Height="30" Margin="0,0,0,1" Click="Clients_Click" />
<Button Content="Hello" Height="30" Margin="0,0,0,1" />
<Button Height="30" Margin="0,0,0,1" >
<Viewbox>
<Image Source="{StaticResource Refresh -M}"/>
</Viewbox>
</Button>
</StackPanel>
<!--PagesPresenterFrame-->
<Frame x:Name="Main" Grid.Column="2" Margin="0,34,0,0" Grid.RowSpan="2"/>
</Grid>
</Border>
You use the relative uri to display the image. In the XAML file, the compiler found the image based on your relative uri and the root path where XAML is located, so the image can be displayed. When you run exe, the father uri is folder where the exe in , so exe cannot find the image in combination with the relative path you provided, the image will not be displayed. Solution: such as exe in Solution/bin/Debug, add Basic/refresh-100. PNG in the Debug folder,use UriSource= "/Basic/refresh-100. PNG” in xaml , run exe , the image will be shown
[Update]
You could set the Copy to the Output Directory as 'Copy if newer' or 'Copy always' in the properties of the picture. See the below picture:

A ListView column with an image in its data template is not visible

I have the following three data templates:
<HierarchicalDataTemplate DataType="{x:Type models:DirOrFileItem}" ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal">
<Image Source="../Images/Folder.png" Stretch="Uniform" Height="20"></Image>
<TextBlock Text="{Binding Name}" ToolTip="{Binding RelativePath}"></TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate x:Key="SelectColTemplate">
<CheckBox IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}" />
</DataTemplate>
<DataTemplate x:Key="IconTemplate">
<Image Source="{Binding ImageData}" Stretch="Uniform" Height="20" />
</DataTemplate>
I set the ImageData property on the row viewmodel as follows:
item.ImageData = ImageHelper.LoadImage(AppConstants.FileRsourcePath);
where LoadImage is:
public static BitmapImage LoadImage(string filename)
{
return new BitmapImage(new Uri("pack://application:,,,/" + filename));
}
I know it loads the image correctly because when I examine the ImageData property after setting it, it has properties that are correct for the image that was loaded.
The HierarchicalDataTemplate is for a TreeView, and the image folder.png appears as and where expected. Then I have the following ListView columns:
<GridViewColumn CellTemplate="{StaticResource SelectColTemplate}" Width="30" />
<GridViewColumn CellTemplate="{StaticResource IconTemplate}" Width="30" />
<GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name" Width="360" />
I am displaying a list of files and folders, and would like an image to highlight which is which.
The CheckBox appears where it should, in the first column, and the third column in the markup appears as the second column on screen, as if the column with the image template, IconTemplate, does not exist.
Both images are where they should be, in the same Images folder, so why is my "image column" not appearing as the second column as I expect?
BONUS QUESTION: If, as #Clemens suggests below in his comment, I set the Build Action of the images to Resource and and load them by Resource File Pack URIs, how would I do so in XAML?
As long you don't need to destroy it....you don't have to load the image, just give the URI in the binding ==> ImageData = new Uri("pack://application:,,,/" + filename)); and the Image control will load it
btw, your viewmodel must not have GUI related element like bitmap image
bye.
Just set Build Action in your image properties to Resource and set full path to your image. It will appear.
http://prntscr.com/jjw4z3

Bind Images in ComboBox using DataTemplate

I am working on an existing code. This thing has got a ComboBox with a few ComboBoxItems. Each items has got a StackPanel withing which there is an Image control and a TextBlock.
Now the source property of the Image control is set to different vector images stored in XAML files whereas the Text property of the TextBlock is set to a localized string.
I want to implement this not by having separate ComboBoxItems but by using DataTemplate. I can create a List of the strings for the TextBlock but I am not able to figure out as to how can I bind the images to the respective Image Controls.
I am open to any other better possible solution. Also, if you think that the right way to do it is the existing one, please let me know.
This might be a duplicate question but I couldn't find one that caters to my issue.
If it is, a link to the other question would suffice.
EDIT: Code Added
<ComboBox x:Name="imageInfoLevelsComboBox" SelectedIndex="1"
Style="{DynamicResource ComboBoxToolBarStyle}"
Margin="6,6,6,0" Width="50"
ToolTip="{x:Static Viewing:ViewingTexts.ImageInformationLevels}"
SelectionChanged="OnImageInfoLevelsComboBoxSelectionChanged" >
<ComboBoxItem x:Name="showAllComboBoxItem"
Style="{DynamicResource ComboBoxItemToolBarStyle}">
<StackPanel Orientation="Horizontal">
<Image x:Name="ImageInfoAllImage"
Source="{StaticResource ImageInfoFullIcon}"
Margin="0,0,4,0"
Width="24" Height="24"/>
<TextBlock
Text="{x:Static Viewing:ViewingTexts.ImageInformationFull}"
Margin="10,0,0,0"
VerticalAlignment="Center"/>
</StackPanel>
</ComboBoxItem>
<ComboBoxItem x:Name="showImportantComboBoxItem"
Style="{DynamicResource ComboBoxItemToolBarStyle}">
<StackPanel Orientation="Horizontal">
<Image x:Name="ImageInfoImportantImage"
Source="{StaticResource ImageInfoLimitedIcon}"
Margin="0,0,4,0"
Width="24" Height="24"/>
<TextBlock
Text="{x:Static Viewing:ViewingTexts.ImageInformationIntermediate}"
Margin="10,0,0,0"
VerticalAlignment="Center"/>
</StackPanel>
</ComboBoxItem>
<ComboBoxItem x:Name="showNotificationsComboBoxItem"
Style="{DynamicResource ComboBoxItemToolBarStyle}">
<StackPanel Orientation="Horizontal">
<Image x:Name="ImageInfoNotificationsImage"
Source="{StaticResource ImageInfoNoneIcon}"
Margin="0,0,4,0" Width="24" Height="24"/>
<TextBlock Text="{x:Static Viewing:ViewingTexts.ImageInformationNone}"
Margin="10,0,0,0" VerticalAlignment="Center"/>
</StackPanel>
</ComboBoxItem>
</ComboBox>
What I think I can do is create a class with 2 objects, one of type string and the other as Image. And then create a list and bind it with the combobox, but the issue is that I am nto sure how to use the vector image as an object.
Thanks.
I think you need to bind a list of objects with at least two properties instead of just a list of strings. One property would contain the string for the text block, the other property would be a urisource for the image.
Refer to this link for examples of urisource definitions Wpf - relative image source path
Bind the ItemsSource of your ComboBox to a collection of objects that has properties representing the text and the image. Then you need to create a IValueConverter and specify an instance of it on the binding of your image that can convert the value of the property on your object to a image source.
Here is one example:
http://www.codewrecks.com/blog/index.php/2010/07/23/bind-an-image-to-a-property-in-wpf/

WPF Create Rectangle Tags on Image from DataBinding

I'm trying to add image tags to a WPF image and I'm not having much luck. I'd like to do it through databinding if at all possible. Can I set a resource with a DataTemplate to take care of this?
Here's what I've been playing with to no avail:
<Image Margin="25,4,14,46"
Name="MainImage"
Stretch="Uniform"
MouseDown="MainImage_MouseDown"
Grid.Row="1"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Source="{Binding Path=FileName}"
>
<Image.Resources>
<DataTemplate DataType="{x:Type capp:CAPMeta}">
<Label Content="{Binding Path=TagText}">
</Label>
</DataTemplate>
</Image.Resources>
</Image>
Thanks!
Well, I ended up using a canvas with ItemResources containing bound text to resolve this issue.

WPF ListBox DataTemplate and Image Question

I have a ListBox with a StackPanel that contains an image and label.
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal" IsItemsHost="True" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<Image Source="{Binding Image}" Cursor="Hand" Tag="{Binding Link}" MouseLeftButtonDown="Image_MouseLeftButtonDown" ToolTip="Click to see this product on adidas.com" VerticalAlignment="Top" HorizontalAlignment="Left" />
<Label Content="{Binding Name}" Cursor="Hand" Tag="{Binding Link}" MouseLeftButtonDown="Label_MouseLeftButtonDown" VerticalAlignment="Bottom" Foreground="White" Style="{StaticResource Gotham-Medium}" FontSize="8pt" HorizontalAlignment="Center" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
I want to show a third image (glow.png) behind the currently moused over image. I can't seem to add a second image to the stack panel, and set it's visibility to hidden. I haven't even tackled the mouseover part yet.
Is adding another image inside the stack panel, and then setting it's visibility to visible the right approach on mouseenter, and then swapping back on mouseleave?
Thanks.
You certainly can have one image behind another. Instead of directly adding the image to your StackPanel, add a Grid and then add both images, like this:
<StackPanel Orientation="Vertical">
<Grid>
<Image Source="..." />
<Image Source="{Binding Image}" ... />
</Grid>
<Label Content="{Binding Name}" ... />
</StackPanel>
You might also like to look into Bitmap Effects, using which you can introduce a "glow" effect onto any WPF element.
Edit: Another way to achieve the effect you want (I believe) is to swap out the image's Source property in a trigger. I'm not going to try to write the XAML from memory here, but you could catch the IsMouseOver property for the image itself, and when it switches to True you could set its Source to the "glowing" version of the image.
Another possibility is to add a border to your image, set the color of the borderbrush to whatever you want and the opacity to 0. In your MouseEnter event handler set the opacity to 1.

Resources