Adding Image in Run Time - wpf

I want to add an image from my Resources to an Image on the press of a Button.
Using Image1.Source = Properties.Resources.MyImage Does not work because the Image1 is of type ImageSource and the Left side is of type Drawing. How can I fix this code to display the image in my resource to the Image on button press?

This seems to work:
In app.xaml:
<Application.Resources>
<Image x:Key="myImage" Source="C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"/>
</Application.Resources>
in MainWindow.xaml:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="myGrid" Margin="5" Width="500">
<StackPanel>
<Button Content="Load Picture" Click="Button_Click_1"/>
<Image x:Name="ImageHolder"/>
</StackPanel>
</Grid>
in button click event:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Image img = Application.Current.Resources["myImage"] as Image;
ImageHolder.Source = img.Source;
}
So in your code you need to do :
Image myImage = Properties.Resources.MyImage as Image;
Image1.Source = myImage.Source;

Related

How to change all WPF Windows background colors?

MainWindow XAML:
<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="300" Width="500">
<Grid>
<Button Width="300" Height="30" Content="Change All Windows Background Colors To Yellow" Name="Button1" VerticalAlignment="Top" />
<Button Width="300" Height="30" Content="Show Me The Window1" Name="Button2" />
</Grid>
</Window>
Window1 XAML:
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="500">
<Grid>
</Grid>
</Window>
MainWindow vb.net codes;
Class MainWindow
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
For Each window As Window In Application.Current.Windows
window.Background = New SolidColorBrush(Colors.Yellow)
Next
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button2.Click
Dim myWindow As New Window1()
myWindow.Owner = Application.Current.MainWindow
myWindow.ShowDialog()
End Sub
End Class
Steps to reproduce:
Run this WPF project.
Click Change All Windows Background Colors To Yellow button.
Click Show Me The Window1 button.
Question: Why doesn't Window1's background color change to yellow?
You can use Style to do this
<Application x:Class="ChangeStyleHelp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style x:Key="MyStyle" TargetType="{x:Type Window}">
<Setter Property="Background" Value="Green" />
</Style>
</Application.Resources>
</Application>
And put below code
private void Button1_Click(object sender, RoutedEventArgs e)
{
Style style = new Style
{
TargetType = typeof(Window)
};
style.Setters.Add(new Setter(Window.BackgroundProperty, Brushes.Yellow));
Application.Current.Resources["MyStyle"] = style;
}
And add style to all windows (parent and child)
<Window x:Class="SimpleWpfPageApp.Window1"
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:SimpleWpfPageApp"
mc:Ignorable="d" Style="{DynamicResource MyStyle}"
Basically you are looking for WPF them just checkout this :
compiled-and-dynamic-skinning
managing-themes-in-wpf

wpf how to change a canvas image Position programmatically

I am a student on my final year and im doing a project regarding WPF but i'm totally new to WPF and i have been assigned some tasks,but im currently stuck on how to change the Image position on the canvas programitically from left to right.below are the codes
<<Window x:Class="changing_bird_position.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<Canvas Name="canvas" Background="LightBlue" Width="500" Height="280" Margin="264,0,28,0">
<Image Source="inlandbird.png" Name="Image" Height="43" Canvas.Left="55" Canvas.Top="22"/>
</Canvas>
<Button Content="Button" Click="Button_Click" Width="50" Height="50" Margin="246,0"/>
</StackPanel>
Xaml.cs
public partial class MainWindow : Window
{
Image img = new Image();
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Canvas.SetLeft(img, 200.0);
Canvas.SetTop(img, 200.0);
In your code behind you don't need to create this instance of an Image (img). You can access the image by it's name property.
I renamed the image in the xaml to make it clear.
XAML:
<Window x:Class="WpfApplication15.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<Canvas Name="canvas" Background="LightBlue" Width="500" Height="280" Margin="264,0,28,0">
<Image Source="inlandbird.png" Name="BirdImage" Height="43" Canvas.Left="55" Canvas.Top="22"/>
</Canvas>
<Button Content="Button" Click="Button_Click" Width="50" Height="50" Margin="246,0"/>
</StackPanel>
</Window>
Code behind:
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Canvas.SetLeft(BirdImage, 200.0);
Canvas.SetTop(BirdImage, 200.0);
}
}
}

How to click a button of the page to change the source of the frame of Windows?

I have a WPF project, I add a frame in the Windows,the source of the frame is the page. I want to achieve clicking a button in the page to change the page of the frame.
<Window x:Class="MagicArm.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MagicArm"
Title="MainWindow">
<Frame Name="FrameContent"Source="PageStart.xaml"></Frame>
</Window>
PageStart:
<Page x:Class="MagicArm.PageStart"
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"
mc:Ignorable="d"
Height="452" Width="800"
Title="PageStart">
<Canvas>
<button name=""> </button>
</Canvas>
EDIT: A functional solution can look like:
MainWindow XAML:
<StackPanel>
<Frame Name="frmMainContent" Height="260"
DataContext="MyPageInformation"
Source="{Binding}"
NavigationUIVisibility="Hidden">
</Frame>
</StackPanel>
MainWindow cs:
frmMainContent.Source = new Uri("test1.xaml", UriKind.Relative); // initialize frame with the "test1" view
test1 XAML:
<Grid>
<Button Click="ButtonBase_OnClick" Background="Red" Height="30" Width="100">Go to page 2</Button>
</Grid>
test1 cs:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
NavigationService ns = NavigationService.GetNavigationService(this);
ns.Navigate(new Uri("test2.xaml", UriKind.Relative));
}
test2 XAML:
<Grid>
<Button Click="ButtonBase_OnClick" Width="100" Height="30" Background="RoyalBlue"> Go to page 1</Button>
</Grid>
test2 cs:
NavigationService ns = NavigationService.GetNavigationService(this);
ns.Navigate(new Uri("test1.xaml", UriKind.Relative));
This is a working solution using NavigationService.

Can not bind View to ViewModel

I'm trying to do a simplest thing: bind a View to ViewModel, but for some reason it doesn't work.
I've got MainWindow:
<Window x:Class="ImagesToAmazon.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"
xmlns:v="clr-namespace:ImagesToAmazon.View"
xmlns:vm="clr-namespace:ImagesToAmazon.ViewModel" >
<Window.Resources>
<DataTemplate DataType="{x:Type vm:MainViewModel}">
<v:MainControl />
</DataTemplate>
</Window.Resources>
<Grid>
</Grid>
MainViewModel:
namespace ImagesToAmazon.ViewModel{
public class MainViewModel {
}}
MainControl:
<UserControl x:Class="ImagesToAmazon.View.MainControl"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<StackPanel>
<Button Content="Button" Height="23" Name="button1" Width="75" />
</StackPanel>
Also, I override App.OnStartup to set MainWindow context:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var window = new MainWindow();
var viewModel = new ViewModel.MainViewModel();
window.DataContext = viewModel;
window.Show();
}
}
All compiles, but I don't see my button from MainControl.
Any clues?
In the main window, replace the Grid with
<ContentControl Content="{Binding }" />
This will attempt to use the current DataContext as the content of the control, and it will use the DataTemplate u defined earlier.

How to correctly pass some custom parameters to event handlers in WPF?

What could be a correct way to assign parameters to objects in WPF window (objects as simple as rectangles) and pass them to event handlers on mouse clicks?
Thank you.
Here's one way:
Put this in the Window:
public static readonly RoutedUICommand clickCommand = new RoutedUICommand("TheCommand", "TheCommand", typeof(Window1));
void ClickRectangle(object sender, ExecutedRoutedEventArgs e)
{
if (e.Parameter == null)
return;
MessageBox.Show("parameter = " + e.Parameter.ToString());
}
Then in the XAML:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="300">
<Grid>
<Rectangle Margin="50" Fill="Blue">
<Rectangle.CommandBindings>
<CommandBinding
Command="my:Window1.clickCommand" Executed="ClickRectangle" />
</Rectangle.CommandBindings>
<Rectangle.InputBindings>
<MouseBinding MouseAction="LeftClick"
Command="my:Window1.clickCommand" CommandParameter="123" />
</Rectangle.InputBindings>
</Rectangle>
</Grid>
</Window>

Resources