Images not appearing on buttons when user control is in referenced assembly - wpf

I have a main WPF application which references a WPF User Control Library that I created. In the library, I have one user control and an images folder (images used by user control). Here is part of the XAML that I have in the user control:
<UserControl x:Class="Alstom.UserControls.MainToolbar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Alstom.UserControls"
mc:Ignorable="d"
d:DesignHeight="120" d:DesignWidth="3440">
<UserControl.Resources>
<Image x:Key="Subway" Source="/images/MainToolbar/Subway.png"/>
<Image x:Key="Globe" Source="/images/MainToolbar/Globe.png"/>
<Image x:Key="Gavel" Source="/images/MainToolbar/Gavel.png"/>
<Image x:Key="Clipboard" Source="/images/MainToolbar/Clipboard.png"/>
<Image x:Key="EllipsisVertical" Source="/images/MainToolbar/EllipsisVertical.png"/>
<Image x:Key="Check" Source="/images/MainToolbar/Check.png"/>
<Image x:Key="Bell" Source="/images/MainToolbar/Bell.png"/>
The user control has buttons which I wrote like the following (note that I defined the Content property as StaticResource:
<Button x:Name="button" Grid.Row="1" Grid.Column="0" Content="{StaticResource Subway}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="48" Height="48"/>
When I double click the user control, in the designer, I see the images on the buttons.
But I created a main WPF application in my solution which references the external assembly like so:
<Window x:Class="MainToolBar.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:myMainToolbar="clr-namespace:Alstom.UserControls;assembly=Alstom.UserControls"
mc:Ignorable="d"
Title="MainWindow" Height="auto" Width="3440" ScrollViewer.VerticalScrollBarVisibility="Visible">
<Grid>
<myMainToolbar:MainToolbar HorizontalAlignment="Left" VerticalAlignment="Top"/>
</Grid>
When I double-click the MainWindow.xaml, I don't see the images on the button in the designer, nor when I run it.
What do I need to do to show the images at runtime? In the user control project, the images have a Build Action = Resource. But I tried to change one of them to Embedded Resource, and it still did not work.
What am I doing wrong?

Try to set the build action of the images to Resource and use a pack URI:
<Image x:Key="Subway" Source="pack://application:,,,/YourAssemblyName;component/images/MainToolbar/Subway.png"/>
Don't forget to change "YourAssemblyName" to the actual name of your assembly/WPF User Control Library project.
Pack URIs in WPF: https://msdn.microsoft.com/en-us/library/aa970069(v=vs.110).aspx

Related

Some Extended Unicode Characters (e.g., Zodiac Symbols) Displaying Differently between UWP and WPF

Certain extended Unicode characters, among which are the Zodiac symbols, are displaying differently in WPF TextBlocks vs. UWP TextBlocks. In UWP, as well as web browsers (for example, ♑), Toast messages, etc., display the characters with a purple background and white foreground. In WPF, the same characters are displayed with the control's background color and a black foreground. Ideally, the characters in WPF apps should be displayed consistently with the way they are displayed in other apps.
The following are the results of two test apps, one created as UWP and the other created as WPF. No changes were made to any of the other files (App.xaml, etc.), apart from the addition of the TextBlock control as follows:
UWP MainPage.xaml:
<Page x:Class="UwpApp1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UwpApp1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Height="150" Width="150">
<Grid>
<TextBlock Text="♑" HorizontalAlignment="Center" Margin="0,0,0,0" VerticalAlignment="Top" FontFamily="Segoe UI" FontSize="48"/>
</Grid>
</Page>
UWP application screen:
WPF MainWindow.xaml:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="150" Width="150">
<Grid>
<TextBlock Text="♑" HorizontalAlignment="Center" Margin="0,0,0,0" VerticalAlignment="Top" FontFamily="Segoe UI" FontSize="48"/>
</Grid>
</Window>
WPF application screen:
Is anyone aware of an application setting or any other change that can be done to the WPF application so it will display these types of special characters consistently with other applications?

Using a properties grid with a tree control

I want to create a tree control that when the user clicks on an element in the tree control, a properties grid is populated with its properties.
I've created a tree control as a user control and I'm using Jaime Oliveras' property grid. I have both on a WPF window but I can't seem to figure out how to make the two communicate with each other.
The XAML on the Main window is:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:advanced="clr-namespace:OptionsStrategyTree.ViewModel"
xmlns:local="clr-namespace:OptionsStrategyTree.ViewModel"
xmlns:Controls="clr-namespace:System.Windows.Controls"
x:Class="OptionsStrategyTree.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="400" Width="500">
<Grid>
<local:UserStrategyTree HorizontalAlignment="Left" Margin="18,6,0,13"
Width="195" Height="340" />
<Controls:WpfPropertyGrid x:Name="StrategyTreePropertyGrid"
HorizontalAlignment="Left" Height="340" Margin="233,6,0,0"
VerticalAlignment="Top" Width="239"/>
</Grid>
</Window>
I've had this properties grid working nicely when the controls are all on the same XAML window. But when I've created a tree control as a user control, it's a separate entity in its own XAML space that seems to be isolated from everything else. Is there any way to connect the two?

Don't understand why the user control we've written doesn't show up in a ModernWindow but does in a window

We're working on a WPF app, using the ModernUI for WPF. And we're also using MVVM Light. We've written some user controls which we intend to bring up in separate windows. Each of the windows are ModernUI's ModernWindow. Whenever we try to bring them up, nothing is shown in the ModernWindow. Out of frustration I thought I'd try doing the same thing in a plain old WPF window. Works perfectly each time. I don't know why it works in a regular WPF window but doesn't work in a ModernWindow. What is wrong???
Here's the XAML for the plain old window:
<Window x:Class="CoreFramework.BozoWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:view="clr-namespace:CoreFramework.View"
xmlns:local="clr-namespace:CoreFramework"
mc:Ignorable="d"
Title="Bozo Window" Height="300" Width="300">
<ContentControl Content="{Binding ProductList, Source={StaticResource Locator}}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
And here's the XAML for the ModernWindow:
<mui:ModernWindow x:Class="CoreFramework.DataViewWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mui="http://firstfloorsoftware.com/ModernUI"
xmlns:vm="clr-namespace:CoreFramework.ViewModel"
xmlns:view="clr-namespace:CoreFramework.View"
xmlns:content="clr-namespace:CoreFramework"
Title="BOTS"
ShowInTaskbar="True"
Closing="ModernWindow_Closing">
<ContentControl Content="{Binding Path=ProductList, Source={StaticResource Locator}}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
One approach you could use is adding Menu links which is built into the Modern UI framework:
<mui:ModernWindow x:Class="ExampleApp.ExampleWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mui="http://firstfloorsoftware.com/ModernUI"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
Title="Scrum Manager" IsTitleVisible="True" ContentSource="/Views/DefaultUserControl.xaml"
x:Name="mainWindow" MenuLinkGroups="{Binding Groups}"
WindowStartupLocation="CenterScreen" WindowState="Maximized">
<i:Interaction.Triggers>
</i:Interaction.Triggers>
<mui:ModernWindow.TitleLinks>
<mui:Link DisplayName="settings" Source="/Views/SettingsWindow.xaml" />
<mui:Link DisplayName="Sign out" Source="/Infrastructure/Logout.xaml" />
</mui:ModernWindow.TitleLinks>
When a link is clicked, it opens up the 'Source' which would be UserControl xaml files.
See Github for more details:
https://github.com/firstfloorsoftware/mui

xaml image not displayed during runtime but displaying during design (VB, wpf) - why?

I'm trying to understand WPF and have created a WPF test project. I've put a monkey01.jpg image in every folder starting from the project's root, moving up in hierarchy, to the debug folder. On the MainWindow I've created an Image and defined the source property to point to the monkey01.jpg and it displays correctly in design view. But when I run it, the image doesn't appear.
If I add the image file to the project as a resource, it will be displayed during runtime which is expected. (But this is not what I'm testing here)
If in the xaml file I write the absolute path as in Source="C:\Users\User\Desktop\visual_studio_projects\WpfApplication1\monkey01.jpg it will be displayed too.
But if I use a relative path (like in the example below), the image won't be displayed. It's as if upon execution, the program doesn't run inside the project folder and its subfolders; Instead it runs in some other location that has no access the image. Why this behaviour? What am I missing?
This is the MainWindow.xaml code:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Image x:Name="Thumbnailtest" Source="monkey01.jpg" />
</Grid>
</Window>
Thank you!
You need to set Copy to Output Directory in the Properties Window for the image to "Copy if newer".
Works fine here when I've set the image to Resource and don't copy, note I changed your monkey image to .png and I created a folder for it(Data/Images).
<Window x:Class="MasoneryLibrary.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MasoneryLibrary;assembly=MasoneryLibrary"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid>
<Image Source="Data/Images/monkey01.png" HorizontalAlignment="left" VerticalAlignment="Top" Width="290" Height="456" Margin="285,0,-58,-136" Stretch="None" StretchDirection="DownOnly"/>
</Grid>
</Grid>
Hmmmm.. is this a library? You should use URI packs to be safe.
With URI pack:
<Image Source="pack://application:,,,/MasoneryLibrary;component/Data/Images/monkey01.png" HorizontalAlignment="left" VerticalAlignment="Top" Width="290" Height="456" Margin="285,0,-58,-136" Stretch="None" StretchDirection="DownOnly"/>
Hope it helps!
Cheers,
Stian

wpf crystalreport viewer dissappear for no reason

Well the problem is easy to explain and hard to figure out, at least by me, so i have a tabcontrol with 3 tabitem, on one of the tabitems i have a wpf reportviewer that loads a report of some data, when i change tabs, it does not matter to each one when i get back to the tab with the crystalreport in it, the crystalreport viewer is not there anytmore! i also have a button on top to refresh the report and the button is still there but the crystal report control is not there anymore!
my xaml looks like this
<UserControl x:Class="uccvrFactuur"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:my="clr-namespace:SAPBusinessObjects.WPF.Viewer;assembly=SAPBusinessObjects.WPF.Viewer"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Button Content="Report" Height="50" HorizontalAlignment="Right" Margin="5" Name="Button1" VerticalAlignment="Top" Width="50" />
<my:CrystalReportsViewer Name="crvFactuur" Margin="0,60,0,0" />
</Grid>
</UserControl>
i must say the first time i click on that tab the control is there but i change tabs and go back to it the control is not there anymore.
Thanks in advance

Resources