I want to bind an image from my Resources without using its path in my code... this line setGreen\Red work fine when I use
This full path
@"C:\Users\hed-b\source\repos\WpfApp4Test\WpfApp4Test\Resources\smile_green.png";
@"C:\Users\hed-b\source\repos\WpfApp4Test\WpfApp4Test\Resources\smile_red.png";
But its not work for using
"Resources/smile_green.png", "Resources/smile_red.png"
or using Resources.smile_red, Resources.smile_green
I have no idea what more I can do to make this happen...
My Pictures are sit in My Project Name/Resources/my Image
Thanks for help!
this is my ViewModel
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using WpfApp4Test.Properties;
namespace WpfApp4Test
{
public class MainWindowVM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string DisplayedImagePath { get; set; }
private ICommand _SetGreen;
public ICommand StartGreen
{
get
{
if (_SetGreen == null)
{
_SetGreen = new RelayCommand((param) => setGreen());
}
return _SetGreen;
}
}
private ICommand _SetRed;
public ICommand StartRed
{
get
{
if (_SetRed == null)
{
_SetRed = new RelayCommand((param) => setRed());
}
return _SetRed;
}
}
private void setGreen() {
//DisplayedImagePath= @"C:\Users\hed-b\source\repos\WpfApp4Test\WpfApp4Test\Resources\smile_green.png";
DisplayedImagePath = "Resources/smile_green.png";
}
private void setRed()
{
//DisplayedImagePath = @"C:\Users\hed-b\source\repos\WpfApp4Test\WpfApp4Test\Resources\smile_red.png";
DisplayedImagePath = "Resources/smile_red.png";
}
}
}
this is my View
<Window x:Class="WpfApp4Test.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:WpfApp4Test"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<UniformGrid Rows="1" Columns="3">
<Image Source="{Binding DisplayedImagePath ,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
<Button Content="Start Green" Command="{Binding StartGreen}"/>
<Button Content="Start Red" Command="{Binding StartRed}" />
</UniformGrid>
</Grid>
</Window>